HE AIR FORCE SCHOOL: SUBROTO PARK: DELHI CANTT. 110010 WT-II CLASS : XI SET I SUB: INFORMATICS PRACTICES ANSWER KEY MAX MARK: 30 TIME ALLOWED : 1Hr 10 Mins 1. (a) What will be the output of the code 2 int f=1,i=2; do f*=i; i=i+1; while(i<5); System.out.println(f); Ans: 24 (b) The following code has some errors. Rewrite the correct code. Underline the corrections made. 2 Float ch; switch(ch); case a : case A ; case e : case E : case i : case i : case u : case U : ++vowels; default : ++others; Ans: char ch; int vowvels,others; switch(ch)_ case a : case A : case e : case E : case I : case i : case u : case U : ++vowels; default : ++others; (c) Rewrite the following program code using a for loop: 2 IP class XI WT2 answer key Page 1 of 10
int i,sum=0;` while(i<10) sum +=i; i+=2; Ans: int i,sum=0; for(i=0;i<10;i+=2) sum +=i; (d) Write the name of the control used for the following 2 i. User has to give a multiline input TEXTAREA ii. Picture to be displayed. LABEL iii. Execution of statements BUTTON iv. A heading is to be displayed LABEL 2. (a) Differentiate between radio button and check box. Give any 1 method common to these controls? 2 RadioButton: Allow us to choose a single item from a group of jradiobutton options. It is circular in shape and a dot appears when selected. CheckBox: Allow us to choose one or more items from a group of jcheckbox options. It is square in shape and a tick appears when selected. METHOD- isselected() (b) Differentiate between do while and while loop with example. 2 While loop : The while loop is an entry-controlled loop. It means that the loop condition is testedbefore executing the loop body. If the loop condition is initially false, for the first iteration, then loop may not execute even once. The syntax of the while loop is as follows: Syntax while(test expression) loop body do while : Do..While loop is an exit-controlled loop. In the do..while loop, the test occurs at the end of the loop. This ensures that the do..while loop executes the statements included in th e loop body at least once. IP class XI WT2 answer key Page 2 of 10
The syntax of the loop is as follows: Syntax : do loop body while (test expression); 3.Create an application in Java using GUI to calculate the largest of 3 floating point numbers? 2 float a = Float.parseFloat(t1.getText()); float b = Float.parseFloat (t2.gettext()); float c = Float.parseFloat (t3.gettext()); if((a>b)&&(a>c)) l1.settext( a is largest ); else if((b>a)&&(b>c)) l1.settext( b is largest ); else l1.settext( c is largest ); 4.Create an application in Java using GUI to find the cube if a float number entered by the user if it is greater than 100 and square of a number if it is less than 100. Take the number as input from the user. 2 float a = Float.parseFloat(t1.getText()); if(a>100) res=a*a*a; else res=a*a; t2.settext( +a); 5.Create an application in Java using GUI to print the table of a number entered by the user. 2 int a = Integer.parseInt(t1.getText()); for (int i=1;i<=10;i++) ta1.append(i*a+ \n ); 6. Create a java application that will print odd numbers from 2 to 100? 2 for (int i=3;i<=100;i++) ta1.append(i+ \n ); 7.Create an application in Java to print the weekday for the corresponding weekday number entered? 3 IP class XI WT2 answer key Page 3 of 10
int day = Integer.parseInt(t1.getText()); switch (day) case 1 : l1.settext( Mon ); case 2 : l1.settext( Tues ); case 3 : l1.settext( Wed ); case 4 : l1.settext( thur ); case 5 : l1.settext( Fri ); case 6 : l1.settext( sat ); case 7 : l1.settext( Sun ); default : l1.settext( wrong number entered ); 8.Write an application in Java i. To calculate the net amount based on the conditions given below. 4 Mode of payment Discount Platinum 20% Silver 15% Gold 10% An additional discount of 5% will be given if the bill amount is more than 50000 IP class XI WT2 answer key Page 4 of 10
jtextfield4.seteditable(false); jtextfield5.seteditable(false); float Offer = 0; // For offer float AddOffer = 0; // For additional offer if (jradiobutton1.isselected()==true) Offer = Float.parseFloat(jTextField2.getText()) * 20/100; else if (jradiobutton2.isselected()==true) Offer = Float.parseFloat(jTextField2.getText()) * 15/100; else if (jradiobutton3.isselected()==true) Offer = Float.parseFloat(jTextField2.getText()) * 10/100; // Displaying the offer amount in jtextfield3 jtextfield3.settext(string.valueof(offer)); // Calculating additional offer amount if (Float.parseFloat(jTextField2.getText()) > 50000) AddOffer = Float.parseFloat(jTextField2.getText()) * 5/100; // Displaying the additionl offer amount in jtextfield4 jtextfield4.settext(string.valueof(addoffer)); jbutton2.setenabled(true); ii. Write the code for clear button to clear all text box contents and disable the calculate discount and calculate net amount button. 2 T1.setText( ); T2.setText( ); T3.setText( ); T4.setText( ); T5.setText( ); Calbutton.setEnabled(false); Calbutton1.setEnabled(false); iii. Write the code for exit button to close the application. 1 System.exit(0); IP class XI WT2 answer key Page 5 of 10
THE AIR FORCE SCHOOL: SUBROTO PARK: DELHI CANTT. 110010 WT-II CLASS : XI SET II SUB: INFORMATICS PRACTICES ANSWER KEY MAX MARK: 40 TIME ALLOWED : 1Hr 10 Mins 1. (a) What will be the output of the code 2 1 int Num = 1; do jtextarea1.settext(num + "\n"); Num = Num + 1; while(num>=10); (c) The following code has some errors. Rewrite the correct code. Underline the corrections made. 2 int sum;value;inct; int i for(i==0;i<=10;i++) sum=sum+i; inct++; system.out.print(inct); Ans : int sum,value,inct; int i; for(i=0;i<=10;i++) sum=sum+i; inct++; System.out.print(inct); (c) Rewrite the following program code using a while loop 2 int i,j; for(i=1;i<=4;i++) System.out.print(j); System.out.println(); Ans: int i=1,j; while(i<=4) System.out.print(j); i++; System.out.println(); IP class XI WT2 answer key Page 6 of 10
(d) Write the name of the control used for the following 2 v. User has to give a multiline input TEXTAREA vi. Select nationality RADIO BUTTON vii. Select gender RADIO BUTTON viii. A heading is to be displayed LABEL 2. (a) Differentiate between Text field and Password field components 2 The Text Field displays the obtained text in unencrypted form and we can extract the text using gettext()whreas password field displays the obtained text in encrypted form. This component allows confidential input like passwords which are single line. and we can extract the text using getpassword() (b) Differentiate between entry controlled and exit controlled loop with example. 2 Entry Controlled loop : It means that the loop condition is tested before executing the loop body. If the loop condition is initially false, for the first iteration, then loop may not execute even once.eg while loop. The syntax of the while loop is as follows: Syntax while(test expression) loop body (iii) Exit Controlled loop: an exit-controlled loop tests the condition occurs at the end of the loop. This ensures that the do..while loop executes the statements includ ed in the loop body at least once. The syntax of the loop is as follows: Syntax : do loop body while (test expression); 9.Create an application in Java using GUI to calculate the smallest of 3 floating point numbers? 2 float a = Float.parseFloat(t1.getText()); float b = Float.parseFloat (t2.gettext()); float c = Float.parseFloat (t3.gettext()); if((a<b)&&(a<c)) l1.settext( a is smallest ); else if((b<a)&&(b<c)) l1.settext( b is smallest ); else l1.settext( c is smallest ); IP class XI WT2 answer key Page 7 of 10
10. Create an application in Java using GUI to find the area of a square if the choice entered by the user is 1 and area of a rectangle if the choice is 2 and should print invalid for any other choice. 2 int choice = Integer.parseInt(t1.getText()); switch(choice) case 1 : float s = Float.parseFloat(t1.getText()); ans = s*s; case 2: float l = Float.parseFloat(t1.getText()); float b = Float.parseFloat(t2.getText()); ans = l*b; default : lans.settext( invalid ); lans.settext( ans= +ans); 11. Create an application in Java using GUI to print the table of a number entered by the user. 2 int a = Integer.parseInt(t1.getText()); for (int i=1;i<=10;i++) ta1.append(i*a+ \n ); 12. Create an application in Java to print all numbers between the 2 numbers entered by the user. 2 int low = Integer.parseInt(t1.getText()); int high = Integer.parseInt(t1.getText()); for (int i=low;i<=high;i++) ta1.append(i+ \n ); 13. Create an application in Java to print the corresponding month name in case the user enters number from 1 to 6 and should print invalid if number entered is more than 6? 3 int day = Integer.parseInt(t1.getText()); switch (day) IP class XI WT2 answer key Page 8 of 10
case 1 : l1.settext( Jan ); case 2 : l1.settext( Feb ); case 3 : l1.settext( Mar ); case 4 : l1.settext( Apr ); case 5 : l1.settext( May ); case 6 : l1.settext( June ); default : l1.settext( invalid ); 14. Write an application for the following. i. Write the coding for CALCULATE button which calculates and displays the discount percentage and discounted price based on the table given below 4 OPTION DISCOUNT % CASH NIL CREDIT CARD 25% IP class XI WT2 answer key Page 9 of 10
float dis,disc; float amt = Float.parseFloat(t1.getText()); if(jradiobutton1.isselected()==true ) Dis=0; else if(jradiobutton2.isselected()==true ) Dis=25; T2.setText(dis+ % ): Disc=amt-amt*dis/100; T3.setText(disc+ ); ii. Write the code for clear button to clear all text box contents and disable the calculate button. 2 T1.setText( ); T2.setText( ); T3.setText( ); Cmdbutton.setEnabled(false); iii. Write the code for exit button to close the application. 1 System.exit(0); IP class XI WT2 answer key Page 10 of 10