SlideShare a Scribd company logo
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

Similar to JAVA PRACTICE QUESTIONS v1.4.pdf

Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
Ankitchhabra28
 
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdfDriver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
anandhomeneeds
 
Studyx4
Studyx4Studyx4
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
adityaraj7711
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Sunil Kumar Gunasekaran
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
anupamfootwear
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
Tak Lee
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Ayes Chinmay
 
The Art of Clean Code
The Art of Clean CodeThe Art of Clean Code
The Art of Clean Code
Yael Zaritsky Perez
 
Parameters
ParametersParameters
Parameters
James Brotsos
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
Niraj Bharambe
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
Katecate1
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
Mahyuddin8
 
Java practical
Java practicalJava practical
Java practical
william otto
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
arishmarketing21
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
ADITYA BHARTI
 

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

Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
 
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf1. import java.util.Scanner; public class Alphabetical_Order {.pdf
1. import java.util.Scanner; public class Alphabetical_Order {.pdf
 
14 thread
14 thread14 thread
14 thread
 
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdfDriver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
 
Studyx4
Studyx4Studyx4
Studyx4
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
The Art of Clean Code
The Art of Clean CodeThe Art of Clean Code
The Art of Clean Code
 
Parameters
ParametersParameters
Parameters
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 
Java practical
Java practicalJava practical
Java practical
 
Refer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdfRefer to my progress on this assignment belowIn this problem you w.pdf
Refer to my progress on this assignment belowIn this problem you w.pdf
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
 

More from RohitkumarYadav80

1.pdf
1.pdf1.pdf
PEA305 workbook.pdf
PEA305 workbook.pdfPEA305 workbook.pdf
PEA305 workbook.pdf
RohitkumarYadav80
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
RohitkumarYadav80
 
atm-facilities-and-equipment.pptx
atm-facilities-and-equipment.pptxatm-facilities-and-equipment.pptx
atm-facilities-and-equipment.pptx
RohitkumarYadav80
 
unit 1 MCQ 316.pdf
unit 1 MCQ 316.pdfunit 1 MCQ 316.pdf
unit 1 MCQ 316.pdf
RohitkumarYadav80
 
1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf
RohitkumarYadav80
 
PEA305 Lec 0 1 (1).pptx
PEA305 Lec  0 1 (1).pptxPEA305 Lec  0 1 (1).pptx
PEA305 Lec 0 1 (1).pptx
RohitkumarYadav80
 
7. CPU_Unit3 (1).pdf
7. CPU_Unit3 (1).pdf7. CPU_Unit3 (1).pdf
7. CPU_Unit3 (1).pdf
RohitkumarYadav80
 
asymptotic_notations.pdf
asymptotic_notations.pdfasymptotic_notations.pdf
asymptotic_notations.pdf
RohitkumarYadav80
 
1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf
RohitkumarYadav80
 

More from RohitkumarYadav80 (10)

1.pdf
1.pdf1.pdf
1.pdf
 
PEA305 workbook.pdf
PEA305 workbook.pdfPEA305 workbook.pdf
PEA305 workbook.pdf
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
atm-facilities-and-equipment.pptx
atm-facilities-and-equipment.pptxatm-facilities-and-equipment.pptx
atm-facilities-and-equipment.pptx
 
unit 1 MCQ 316.pdf
unit 1 MCQ 316.pdfunit 1 MCQ 316.pdf
unit 1 MCQ 316.pdf
 
1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf
 
PEA305 Lec 0 1 (1).pptx
PEA305 Lec  0 1 (1).pptxPEA305 Lec  0 1 (1).pptx
PEA305 Lec 0 1 (1).pptx
 
7. CPU_Unit3 (1).pdf
7. CPU_Unit3 (1).pdf7. CPU_Unit3 (1).pdf
7. CPU_Unit3 (1).pdf
 
asymptotic_notations.pdf
asymptotic_notations.pdfasymptotic_notations.pdf
asymptotic_notations.pdf
 
1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf1. Combinational Logic Circutis with examples (1).pdf
1. Combinational Logic Circutis with examples (1).pdf
 

Recently uploaded

JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
Javier Lasa
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
VivekSinghShekhawat2
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 

Recently uploaded (20)

JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptxInternet-Security-Safeguarding-Your-Digital-World (1).pptx
Internet-Security-Safeguarding-Your-Digital-World (1).pptx
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 

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