SlideShare a Scribd company logo
1 of 14
Download to read offline
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.pdfeyewatchsystems
 
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 {.pdfAnkitchhabra28
 
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.pdfanandhomeneeds
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptxadityaraj7711
 
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.pdfanupamfootwear
 
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
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iiiNiraj Bharambe
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docxKatecate1
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.pptMahyuddin8
 
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.pdfarishmarketing21
 
Computer java programs
Computer java programsComputer java programs
Computer java programsADITYA 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

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

VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Roomdivyansh0kumar0
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Roomdivyansh0kumar0
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girladitipandeya
 
Russian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service ThaneRussian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service ThaneCall girls in Ahmedabad High profile
 
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service PuneVIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service PuneCall girls in Ahmedabad High profile
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...APNIC
 
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一3sw2qly1
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Delhi Call girls
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Roomgirls4nights
 
象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdf
象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdf象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdf
象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdfkeithzhangding
 
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Call Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our Escorts
Call Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our EscortsCall Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our Escorts
Call Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our Escortsindian call girls near you
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 
Complet Documnetation for Smart Assistant Application for Disabled Person
Complet Documnetation   for Smart Assistant Application for Disabled PersonComplet Documnetation   for Smart Assistant Application for Disabled Person
Complet Documnetation for Smart Assistant Application for Disabled Personfurqan222004
 
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya Shirtrahman018755
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)Damian Radcliffe
 

Recently uploaded (20)

VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
 
Call Girls In South Ex 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Ex 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In South Ex 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In South Ex 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girls
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
 
Russian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service ThaneRussian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
 
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service PuneVIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
VIP Call Girls Pune Madhuri 8617697112 Independent Escort Service Pune
 
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls KolkataVIP Call Girls Kolkata Ananya 🤌  8250192130 🚀 Vip Call Girls Kolkata
VIP Call Girls Kolkata Ananya 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
定制(CC毕业证书)美国美国社区大学毕业证成绩单原版一比一
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
 
象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdf
象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdf象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdf
象限策略:Google Workspace 与 Microsoft 365 对业务的影响 .pdf
 
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
 
Call Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our Escorts
Call Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our EscortsCall Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our Escorts
Call Girls in East Of Kailash 9711199171 Delhi Enjoy Call Girls With Our Escorts
 
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Alwarpet Phone 🍆 8250192130 👅 celebrity escorts service
 
Complet Documnetation for Smart Assistant Application for Disabled Person
Complet Documnetation   for Smart Assistant Application for Disabled PersonComplet Documnetation   for Smart Assistant Application for Disabled Person
Complet Documnetation for Smart Assistant Application for Disabled Person
 
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
 
How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)How is AI changing journalism? (v. April 2024)
How is AI changing journalism? (v. April 2024)
 

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