Page 1 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, , ASSIGNMENT SHEET, IF-ELSE-IF, Choose the best option for Question 1 to 7:, Question 1:, Which of the following are conditional constructs?, 1., if-else, 2., if-else-if ladder, 3., switch statement, 4., All of these, Question 2:, Which operator cannot be used with if-else statement?, 1. <=, 2. ||, 3. &&, 4. ? :, Question 3:, What will be the output of the following code?, int size = 2;, if (size < 0), System.out.println("Small");, else if (size == 0), System.out.println("Medium");, else, System.out.printIn("Large");, 1., 2., 3., 4., , Small, Large, Medium, Runtime error, , Question 4:, Predict the output of the following code snippet:, int a = 1;, int b = 2;, if (a == b), System.out.println ("Both values are equal");, else, System.out.println ("Values are not equal");, 1. Both values are equal, 2. Incorrect use of the == operator
Page 2 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, 3. Values are not equal, 4. No output, Question 5:, Consider the following code snippet:, if ( c > d), x = c;, else, x = d;, Choose the correct option if the code mentioned above is rewritten using the, ternary operator:, 1. x = (c >d) ? c : d;, 2. x = (c >d) ? d : c;, 3. x = (c >d) ? c : c;, 4. x = (c >d) ? d : d;, Question 6:, if ((a > b) && (a > c)), then which of the following statements is true?, 1. a is the largest number., 2. b is the largest number., 3. c is the largest number., 4. b is the smallest number., Question 7:, A sequence of statements enclosed between a pair of curly brackets is called, 1. a compound statement, 2. an empty statement, 3. a null statement, 4. a void statement, =====================================================, Question 8:, Format the following if statements with indentation:, i. if (x == y) if (x == z) x = 1; else y = 1; else z = 1;, Answer, if (x == y), if (x == z), x = 1;, else, y = 1;, else, z = 1;, ii. if (x == y) {if (y == z) x = 1; y = 2; } else z = 1;, Answer, if (x == y), {
Page 3 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, if (y == z), x = 1;, y = 2;, }, else, z = 1;, iii. if (num1 != num2) {, if (num2 >= num3) x = 1; y = 2; }, else {x = 1; if (num1 == num2) z = 3;}, Answer, if (num1 != num2), {, if (num2 >= num3), x = 1;, y = 2;, }, else, {, x = 1;, if (num1 == num2), z = 3;, }, Question 9:, Write an if statement to find the smallest of the three given integers using the, min() method of the Math class., Answer, if (a < Math.min(b, c)), System.out.println(a);, else, System.out.println(Math.min(b, c));, Question 10:, Find the error in the given statement:, int x = (a => b) ? "a" : "b";, Answer, 1. => is an invalid operator. it should be >=, 2. Type of x should be String., Corrected Statement, String x = (a >= b) ? "a" : "b";, Question 11:, Find the error, if any, in the following code. Write the correct statement., int a=5, b=10;, int x = (a>b)>true:false;
Page 4 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, Answer, 1. Ternary operator is written incorrectly., 2. Type of x should be boolean., Corrected Statement, int a=5, b=10;, boolean x = (a>b) ? true:false;, Question 12:, Rewrite the following statement using if else:, int max=215, min=323;, String str= (max>min) ? "Max is greater than Min" : "Min is Greater than Max";, Answer, int max=215, min=323;, String str="";, if (max > min), str = "Max is greater than Min";, else, str = "Min is Greater than Max";, Question 13:, What will be the value of 'n' after the execution of the code given below?, int x=2, m=1, c=-1;, int n = x + c;, n = n - c + x;, System.out.println(n);, Answer, n will be 4 after execution of the code., First n = x + c is executed ⇒ n = 2 + (-1) = 1., Next, n = n - c + x is executed., n=n-c+x, ⇒ n = 1 - (-1) + 2, ⇒n=4, So final value of n is 4., Question 14:, What will be the output of the following code?, int x=2,y=5,a=0;, a=x;, x=y;, y=a;, System.out.println("x=" + x + " y=" + y);, Answer, Output is:, x=5 y=2
Page 5 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, This code is swapping the values of x and y., Question 15:, Create a program to find out if the number entered by the user is a two, three, or four digits number., Sample input: 1023, Sample output: 1023 is a 4 digit number., Answer, import java.util.*;, public class Number, {, public static void main( ), {, Scanner in = new Scanner(System.in);, System.out.print("Enter number: ");, int n = in.nextInt();, if (n >= 10 && n <= 99), System.out.println("Two Digit Number");, else if (n >= 100 && n <= 999), System.out.println("Three Digit Number");, else if (n >= 1000 && n <= 9999), System.out.println("Four Digit Number");, else, System.out.println("Please enter a 2, 3 or 4 digit number");, }, }, Question 16:, Write a program in Java to read three integers and display them in descending, order., Answer, import java.util.Scanner;, public class Descending, {, public static void main( ), {, Scanner in = new Scanner(System.in);, System.out.print("Enter first number: ");, int a = in.nextInt();, System.out.print("Enter second number: ");, int b = in.nextInt();, System.out.print("Enter third number: ");, int c = in.nextInt();, int max = -1, mid = -1, min = -1;
Page 6 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, if (a > b), {, if (a > c), {, max = a;, if (b > c), {, mid = b;, min = c;, }, else, {, mid = c;, min = b;, }, }, else, {, max = c;, mid = a;, min = b;, }, }, else {, if (b > c) {, max = b;, if (a > c) {, mid = a;, min = c;, }, else {, {, mid = c;, min = a;, }, }, }, else {, max = c;, mid = b;, min = a;, }, }, System.out.println("Numbers in Descending order:");, System.out.println(max + " " + mid + " " + min);
Page 7 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, }, }, Question 17:, Using the ternary operator, create a program to find the largest of three, numbers., Answer, import java.util.Scanner;, public class LargestNumber, {, public static void main( ), {, Scanner in = new Scanner(System.in);, System.out.print("Enter first number: ");, int a = in.nextInt();, System.out.print("Enter second number: ");, int b = in.nextInt();, System.out.print("Enter third number: ");, int c = in.nextInt();, int max = a > b ? a : b;, max = max > c ? max : c;, System.out.print("Largest Number = " + max);, }, }, Question 18:, Employees at Arkenstone Consulting earn the basic hourly wage of Rs.500. In, addition to this, they also receive a commission on the sales they generate, while tending the counter. The commission given to them is calculated, according to the following table:, , Total Sales, , Commission Rate, , Rs. 100 to less than Rs. 1000, , 1%, , Rs. 1000 to less than Rs. 10000, , 2%, , Rs. 10000 to less than Rs. 25000, , 3%, , Rs. 25000 and above, 3.5%, Write a program in Java that inputs the number of hours worked and the total, sales. Compute the wages of the employees.
Page 8 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, Answer, import java.util.Scanner;, public class commission, {, public static void main( ), {, Scanner in = new Scanner(System.in);, System.out.print("Enter number of hours: ");, int hrs = in.nextInt();, System.out.print("Enter total sales: ");, int sales = in.nextInt();, double wage = hrs * 500;, double c = 0;, if (sales < 100), c = 0;, else if (sales < 1000), c = 1;, else if (sales < 10000), c = 2;, else if (sales < 25000), c = 3;, else, c = 3.5;, double comm = c * sales / 100.0;, wage += comm;, System.out.println("Wage = " + wage);, }, }, Question 19:, Mayur Transport Company charges for parcels as per the following tariff:, Weight, , Charges, , Upto 10 Kg., , Rs. 30 per Kg., , For the next 20 Kg., , Rs. 20 per Kg., , Above 30 Kg., Rs. 15 per Kg., Write a program in Java to calculate the charge for a parcel, taking the weight, of the parcel as an input., import java.util.Scanner;
Page 9 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, public class transport, {, public static void main( ), {, Scanner in = new Scanner(System.in);, System.out.print("Enter parcel weight: ");, double wt = in.nextDouble();, double amt = 0;, if (wt <= 10), amt = 30 * wt;, else if (wt <= 30), amt = 300 + ((wt - 10) * 20);, else, amt = 300 + 400 + ((wt - 30) * 15);, System.out.println("Parcel Charge = " + amt);, }, }, Question 20:, Write a program in Java to accept three numbers and check whether they are, Pythagorean Triplet or not. The program must display the message accordingly., [Hint: h2=p2+b2], Answer, import java.util.Scanner;, public class Triplet, {, public static void main( ), {, Scanner in = new Scanner(System.in);, System.out.print("Enter 1st number: ");, int a = in.nextInt();, System.out.print("Enter 2nd number: ");, int b = in.nextInt();, System.out.print("Enter 3rd number: ");, int c = in.nextInt();, if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a), System.out.println("Pythagorean Triplets");, else, System.out.println("Not Pythagorean Triplets");, }, }
Page 10 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, Question 21:, Star mall is offering discount on various types of products purchased by its, customers. Following table shows different type of products and their, respective code along with the discount offered. Based on the code entered, amount entered, the mall is calculating the total amount after deducting the, availed discount. Create a program to calculate total amount to be paid by the, customer., Item, , Item Code, , Discount, , Laptop, , L, , 5%, , LCD, , D, , 7%, , XBox, , X, , 10%, , Printer, , P, , 11%, , Answer, import java.util.Scanner;, public class StarMall, {, public static void main(), {, Scanner in = new Scanner(System.in);, System.out.print("Enter purchase amount: ");, double amt = in.nextDouble();, System.out.print("Enter item code: ");, char code = in.next().charAt(0);, int d = 0;, if(code=='L'), d = 5;, else if(code==’D’), d = 7;, else if(code==’X’), d = 10;, else if(code==’P’), d = 11;, else, System.out.println("Invalid Item Code");, double disc = amt * d / 100.0;, double total = amt - disc;, System.out.println("Total amount payable = " + total);, }, }
Page 11 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, , Question 22:, The City Library charges late fine from members if the books were not returned, on time as per the following table:, Number of days, late, , Magazines fine, per day, , Text books fine, per day, , Up to 5 days, , Rs. 1, , Rs. 2, , 6 to 10 days, , Rs. 2, , Rs. 3, , 11 to 15 days, , Rs. 3, , Rs. 4, , 16 to 20 days, , Rs. 5, , Rs. 6, , More than 20, days, , Rs. 6, , Rs. 7, , Using the if statement, write a program in Java to input name of person,, number of days late and type of book — 'M' for Magazine and 'T' for Text book., Compute the total fine and display it along with the name of the person., Answer, import java.util.Scanner;, public class Library, {, public static void main( ), {, Scanner in = new Scanner(System.in);, System.out.print("Enter name: ");, String name = in.nextLine();, System.out.print("Days late: ");, int days = in.nextInt();, System.out.println("Type M for Magazine");, System.out.println("Type T for Text book");, System.out.print("Enter book type: ");, char type = in.next().charAt(0);, int fine = 0;, if(type==’M’), {, if (days <= 5), fine = 1;, else if (days <= 10), fine = 2;, else if (days <= 15), fine = 3;, else if (days <= 20)
Page 12 :
[VXL TUTORIAL COMPUTER CLASSES BY VINOD KHARE] Ph: 9336061496, fine = 5;, else, fine = 6;, }, else if(type=='T'), {, if (days <= 5), fine = 2;, else if (days <= 10), fine = 3;, else if (days <= 15), fine = 4;, else if (days <= 20), fine = 6;, else, fine = 7;, }, else, System.out.println("Invalid book type");, int totalFine = fine * days;, System.out.println("Name: " + name);, System.out.println("Total Fine: " + totalFine);, }, }