Logical Test Questions for Java/BlueJ |
(Note: All programs codes are Tested in BlueJ) |
[Set I] |
Time: 1:30 Hrs Marks: 4x5=20 |
1. Write a program to accept a double-digit number (not constant) and display a message "Digits are same" if the both digit is same, if the given number is not a double digit number then display an error message "Invalid Number". |
class test1q1 { public static void main(int x) { if(x<10 || x>99) { System.out.println("Invalid Number (must be a double digit)"); } else { if(x%11==0) System.out.println("Both digits are same"); else System.out.println("digits are different"); } } } |
2. Write a program to calculate the sum of first ten multiples of 3 and sum of first ten multiples of 5 separately (using a single while loop) and then display the result. |
class test1q2 { public static void main() { int i=1,s1=0,s2=0; while(i<=10) { s1=s1+(i*3); s2=s2+(i*5); i++; } System.out.println("Sum of 10 Multiples of 3 is : "+s1); System.out.println("Sum of 10 Multiples of 5 is : "+s2); } } |
3. Write a program to enter the three sides of a triangle. Decide whether it is a scalene, isosceles or equilateral triangle. |
class test1q3 { public static void main(int a, int b, int c) { if (a==b && b==c && c==a) System.out.println("Equileteral Triangle"); else if (a!=b && b!=c && c!=a) System.out.println("Scalean Triangle"); else System.out.println("Isoceles Triangle"); } } |
4. Write a program to accept a number (not constant) then check the number is perfect number or not. |
class test1q4 { public static void main(int n) { int i,s=0; for(i=1;i<n;i++) { if(n%i==0) s=s+i; } if(n==s) System.out.println("The given no. "+n+" is a Perfect no"); else System.out.println("The given no. "+n+" is not a Perfect no"); } } |
5. Write a program to display the output in the following format. (Using Nested Loops) 5 45 345 2345 12345 |
class test1q5 { public static void main() { int i,j; for(i=5;i>=1;i--) { for(j=i;j<=5;j++) { System.out.print(j); } System.out.println(); } } } |
[Set II] |
Time: 1:30 Hrs Marks: 4x5=20 |
1. Write a program to find the sum of the series given below 12 + 2 + 32 + 4 + 52 + 6 + 72 + 8 + 92 + 10 |
class test4q1 { public static void main() { int s=0; for(int i=1;i<=10;i++) { if(i%2==0) s=s+i; else s=s+(i*i); } System.out.print("Sum is "+s); } } |
2. Write a program to accept 3 numbers and find the highest using ternary operator. |
class test4q2 { public static void main(int a,int b,int c) { int max=(a>b) ? ((a>c)?a:c) : ((b>c)?b:c); System.out.print("The Highest Number is : "+max); } } |
3. The telephone department wishes to compute monthly telephone bills for its customers using the following rules. Minimum Rs. 250 for first 80 calls, plus 60 paisa per unit for next 80 calls, plus 50 paisa per unit for next 120 calls, plus 40 paisa per unit for any units above 280. Write a program to input number of calls and compute total bill amount. |
class test4q3 { public static void main(int calls) { double bill=0; if(calls<=80) bill=250; else if(calls>80 && calls<=160) bill=250+(calls-80)*0.60; else if(calls>160 && calls<=280) bill=250+48+(calls-160)*0.50; else if(calls>280) bill=250+48+60+(calls-280)*0.40; System.out.print("The Bill Amount is Rs. "+bill); } } |
4. Write a program to accept a number, check whether the numbers is prime number or not. If number is prime, then display "PRIME NUMBER" or display "NOT A PRIME NUMBER". |
class test4q4 { public static void main(int n) { int i,f=0; for(i=2;i<n;i++) { if(n%i==0) { f=1; break; } } if(f==0) System.out.print("Prime number"); else System.out.print("not a Prime number"); } } |
5. Write a program to display the output in the following format. 54321 5432 543 54 5 |
class test4q5 { public static void main() { int i,j; for(i=1;i<=5;i++) { for(j=5;j>=i;j--) { System.out.print(j); } System.out.println(); } } } |
[Set III] |
Time: 1.5 Hrs Marks: 4x5=20 |
1. Generate all elements (upto hundred) of a FIBONACCI series, which is also a prime number. |
class test2q1 { public static void main() { int a=0,b=1,c,i,f; System.out.println("The all terms of Fibonacci Series upto 100 (which is also a Prime number)"); while(a<=100) { f=0; for(i=2;i<a;i++) { if(a%i==0) f=1; } if(f==0) System.out.print(a+" "); c=a+b; a=b; b=c; } } } |
2. Write a program to input a time in second's and convert it into Hours, Minute and Seconds and print the results in following format. (E.g. If the given time is 12035 seconds, then it will be printed as 3H: 20M: 35S) |
class test2q2 { public static void main(int s) { int h=s/3600; s=s%3600; int m=s/60; s=s%60; System.out.print(h+"H: "+m+"M: "+s+"S"); } } |
3. Write a program to accept a number and then check whether first and last digit of the given number is same or not. Then print a message either "Both end is same" or "End is not same". (Note: Program should check that the given number must have at least two digit, if not then print a error message, otherwise solve the given problem). |
class test2q3 { public static void main(int n) { if(n>9) { int ld=n%10; int d=0; while(n>0) { d=n%10; n/=10; } if(d==ld) System.out.print("Both end is same"); else System.out.print("Both end is not same"); } else System.out.print("Number must be atleast two digits"); } } |
4. Write a program to display the following output. (Using Nested Loops)
1 2 4 3 5 7 4 6 8 10 5 7 9 11 13 |
class test2q4 { public static void main() { int i,j; for(i=1;i<=5;i++) { for(j=i;j<=3*i-2;j+=2) System.out.print(j+" "); System.out.println(); } } } |
[Set IV] |
Time: 1:30 Hrs Marks: 4x5=20 |
1. There is only one number upto one Crore, which is divisible by 11 and Number/11 is equal to the sum of each digit present in that number. Write a program to find out that special number. |
class test3q1 { public static void main() { int i,s,n,d,k; System.out.println("The nos upto 1 Crore "); for(n=100;n<=10000000;n++) { if(n%11==0) { k=n; s=0; while(k>0) { d=k%10; s=s+d; k=k/10; } if(n/11==s) System.out.println(n); } } } } |
2. Write a program to find the LCM of two given numbers. |
class test3q2 { public static void main(int a, int b) { int lcm=(a>b)?a:b; while(lcm<=a*b) { if(lcm%a==0 && lcm%b==0) break; lcm++; } System.out.println("The LCM of two nos "+a+" & "+b+" is "+lcm); } } |
3. Write a program to accept two numbers and convert it to a single number after joining them. Suppose the given numbers are 20 and 145 it will display 20145. (You can't print the numbers side by side, you have to apply some logic.) |
class test3q3 { public static void main(int a, int b) { int c=0,d,k=b; while(k>0) { d=k%10; c=c+1; k=k/10; } int no=(a*(int)Math.pow(10,c))+b; System.out.println("The joined number is : "+no); } } |
4. Write a program to display the following output. (Using two nested loops only)
1 2 6 3 7 10 4 8 11 13 5 9 12 14 15 |
class test3q4 { public static void main() { int i,j,a; for(i=1;i<=5;i++) { a=i; for(j=1;j<=i;j++) { System.out.print(a+" "); a=a+(5-j); } System.out.println(); } } } |
[Set V] |
Time: 1:30 Hrs Marks: 5x5=25 |
Q1. The number 151 is a prime palindrome, because it is both prime number and palindrome. Write a class that accept a number from user and print the message "prime palindrome no." or "not prime palindrome no." accordingly. |
import java.io.*; class test5q1 { public static void main() throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader x=new BufferedReader(isr); int num; System.out.print("Enter a Number : "); num=Integer.parseInt(x.readLine()); int f=0; for(int i=2;i<num;i++) if(num%i==0) f++; if(f==0) { int k=num,rev=0,d; while(k>0) { d=k%10; rev=rev*10+d; k=k/10; } if(num==rev) System.out.println("Prime Palindrome"); else System.out.println("Not Prime Palindrome"); } else System.out.println("Not Prime Palindrome"); } } |
Q2. Write a program to find the LCM of two given numbers. |
class test5q2 { public static void main(int a, int b) { int lcm=(a>b)?a:b; while(lcm<=a*b) { if(lcm%a==0 && lcm%b==0) break; lcm++; } System.out.println("The LCM of two nos "+a+" & "+b+" is "+lcm); } } |
Q3. Indian Railway Stand decided to take charge for the
parking of two wheeler/3 wheeler/4wheller as per the following criteria: No. of Hrs. Parked Charge upto 8 hrs Rs 10 next 8 hrs Rs. 6 for additional 8 hours above 16 hours Rs. 5 for each addition 8 hrs. Input number of hours parked and prints the charges as per given criteria. |
Q4. Write a program to accept two numbers from user and convert it to a single number after joining them. Suppose the given numbers are 20 and 145 it will display 20145. (You can't print the numbers side by side, you have to apply some logic.) |
class test5q4 { public static void main(int a, int b) { int c=0,d,k=b; while(k>0) { d=k%10; c=c+1; k=k/10; } int no=(a*(int)Math.pow(10,c))+b; System.out.println("The joined number is : "+no); } } |
Q5. There are 55 employees in an organization. You have
to display the number of employees getting Net Salary above 20000 by
taking only Basic Salary as input and perform the calculation according
to the following given table. Basic DA(% basic salary) IT(% Gross Salary) Below 5000 8% 6% 5000 to < 10000 15% 9% 10000and above 18% 12% Where D.A. and IT are Dearness Allowance and income Tax respectively. Gross Salary = Basic Salary + DA Net Salary = Gross Salary - IT |
Q6. Write a program to display the following output. 1 2 6 3 7 10 4 8 11 13 5 9 12 14 15 |
class test3q4 { public static void main() { int i,j,a; for(i=1;i<=5;i++) { a=i; for(j=1;j<=i;j++) { System.out.print(a+" "); a=a+(5-j); } System.out.println(); } } } |