Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 40

Worksheet 1 - Netbeans

(Question Based on methods, properties and events)


Q1. Which property is used to change the title of JFrame form?

Q2. Which property sets an action to be performed when the user attempts to close the form?

Q3. What are container controls? Give example.

Q4. Differentiate between properties and methods.

Q5. What are events in java programming language?

Q6. Write code to make the jButton1 invisible (hidden).

Q7. Write code to make the jButton2 disabled.

Q8. Write code to make the jButton1 enabled.

Q9. Write code to make the jButton2 visible.


Worksheet 2 Netbeans
(Questions Based on jTextField and jLabel)
Q1. Write code to make the jTextField1 uneditable.

Q2. Ms. Samihta has developed a Java application through which the students of her school can
view their marks by entering their admission number. The marks are displayed in various text
fields. What should she do so that the students are able to view but not change their marks in
text fields?

Q3. Which property is used to change the text in a jLabel or jtextField?

Q4. Which property is used to change the alignment of a text in jTextField?

Q5. What is the use of toolTipText property in JTextField?

Q6. Can jLabel control display an image?

Q7. Write code to make jButton1 invisible and jButton2 disabled.

Q8. Write code to fetch the value of jTextField1 and display the same in jLabel1.

Q9. Write code to display the text “Good Morning” in jTextField1


Worksheet 3 Netbeans (Questions Based on jTextField,JLabel and JTextArea)
Q1. Differentiate between JTextArea and JTextField Object.

Q2. Which property is used to wrap the text in JTextArea Object.

Q3. Which two properties are used to wrap the text to new line?

Q4. Which property in conjunction with linewrap is used to wrap the word instead of letters in
JextArea?

Q5. Write the output of following:


jTextArea1.setText(“This is a \t JTextArea \n Control”);

Q6. Write the output of following lines of code:


jTextArea1.append(“Good”);
jTextArea1.append(“Morning”);

Q7. What is the difference between append and setText method of JtextArea object?

Q8. Write code to fetch the value of jTextField1 and display the same in jLabel1.

Q9. Which property of jTextField and jLabel is used to align the text to center?

Q10. Write any two common properties of JLabel, JTextField and JTextArea.

Q11. Write output of:


jTextArea1.append(“CBSE\n-Central Board of Secondary Education”);
jTextArea1.append(“\nSQL\tStructured Query Language”);
Worksheet 4 Netbeans
(jTextField,JLabel, JTextArea, JPassword)
Q1. Differentiate between JTextArea and JTextField Object.

Q2. Differentiate between JTextField and JPassword Object.

Q3. Which property is used to set the character that will be displayed instead of the actual text?

Q4. Write two properties of JTextArea to auto wrap the words to new line.

Q5. Write the output of following:


jTextArea1.setText(“Netbeans\tis a\nGUI”);

Q6. Write code to fetch the value of jTextField1 and display the same in jLabel1.

Q7. Write code to fetch the contents of JTextField1 and display it in jTextArea1 without deleting
the previous content of jTextArea1.

Q8. Write output of:


jTextArea1.append(“CBSE\n-Central Board of Secondary Education”);
jTextArea1.append(“\nSQL\tStructured Query Language”);

Q9. What are variables in Java?

Q10. Write code to


a) store 20 in integer type variable
b) store 20.5 in a variable
c) store the character Z in a variable

Q11. Identify the error in following program of code:


{
int x = 20;
int x = 50;
}

Q12. Identify the error in following program of code:


double x=20;
{
double x=60;
}

Q13. Write code to fetch the value of jTextField1 and store it in a variable str1.

Q14. Help Manish in identifying the incorrect variable name with justification
from the following:
i. unit@price; ii. fee; iii. userid; iv. avg marks;

Q15. Consider the statement : 1


fname = “Rishabh”;
(i) What is the datatype of fname ?
(ii) Is 456 the same as “456” ? Give reason.
Worksheet 5 Netbeans
(Operators & Conversion Methods)

Q1. What is an assignment operator?

Q2. Write the output of:


int x=5, y=21,z;
z=21%5;
jTextField1.setText(“x=”+x+” y=”+y + “ z=”+z);

Q3. Write output of:


int num1=51;
jTextArea1.append(num1++ +”\n”+ ++num1);

Q4. Write output of:


int num1=51;
int num2=2;
num2*=num1;
jTextArea1.append(num1 +”\n”+ num2);

Q5. Write output of:


int i=63,j=i++,k=++i;
jTextField1.setText(“i=”+i + “,j=”+j+”,k=”+k);

Q6. Write the output of:


double x=15;
x*=3 + 5;
jTextField1.setText(“” +x);

Q7. Write a code in java that takes two numbers as input from textfields and displays their sum in
third textfield.

Q8. Write code in Java that takes principal, rate, and time as input from textfields and displays
simple interest in fourth textField.
Q9. A phone number, consisting of 10 digits, is stored in a string variable strPhone. Now it is
required to store this phone number in a Long type variable lngPhone. Write a Java statement
to do this.

Q10. How one can make a Text Field un-editable on a Frame?

Q11. Write Java code that takes the cost of a pencil from jTextField1 and number of pencils from
jTextField2 and calculates total amount as cost*number to be displayed in jTextField3 and 20%
service tax out of total amount in jTextField4.

Q12. Write Java code that takes value for a number (n) in jTextField1 and cube (n*n*n) of it to be
displayed in jTextField2.

Q13. What will be displayed in jTextAreal after executing the following statement?
jTextAreal,setText ("GREAT\nCOUNTRY\tINDIA") ;

Q14. What will be displayed in jTextfield1 after the execution of the following code ?
int Sum,One=3,Two=5;
Sum = One + Two++ ;
jTextFieldl . setText (Integer.toString (Sum)) ;
jTextField2.setText (Integer . toString (Two));

Q15. Is a string containing a single character same as a


char?

Q16. Write a statement in Java to declare a String type variable with a name
City.
Q17. Distinguish between ‘/’ and ‘%’ operator.

Q18. Explain the usage of the following:


1) int x = 10;
2) if (x==10)
jTextField1.setText(“Value is Ten”);

Q19. An worker_Id consisting of 4 digits is stored in a string variable strWrkld. Now Mr. Jai wants to
store this Id in integer type of variable IntWrkld. Write a Java statement to do this.

Q20. Write Java code to assign the value 70 to variable y. Then decrease the value of y by 5 and
store it in variable z.

Q21. Write Java code to assign the value 10 to variable x and store its square value in another
variable y

Q22. Write Java code (statements) to declare Y as integer variable. Then, assign the value 30
to a variable Y. Increase the value of Y by 5 and store the increased value in Z.

Q23. Shalini is learning Java programming language, as it allows developers to write code that can
run on any machine, regardless of architecture or platform. Help her in finding solutions of her
problems:
I. She wants to compare two integer values for equality in her java code. Out of (=) and (= =),
which operator she should use and why?
II. Help her in writing Java code to initialize two integer variables named num1 and num2 with
values 5 and 10 respectively. Increase the value of num1 by 5 and decrease the value of num2
by 5.

Q24. Item code consisting of 5 digits is stored in an integer type variable intltemCode. Mr. Srikant
wants to store this Item code in a String type variable called strltemCode. Write appropriate
Java statement(s) to help her in performing the same.

Q25. Write Java code to declare a variable named Price of integer type. Assign a value 10 to this
variable. Overwrite the value of price with its double value. Decrease the value of price by 5.

Q26. Write the value that will be assigned to variable x after executing the following
statement :
x = 3 + 36/12 + 2*5;
Q27. Distinguish between parselnt() and parseDouble() methods.

Q28. Write the values of num2 and num3 after execution of following code :
int num1, num2, num3;
num1 = 100;
num2 = ++num1;
num3 = num2++ + ++num1;

Q29. What will be displayed in jTextField1 and jTextField2 after the following code is executed:
int t;
int s;
s = 2;
t = (4*s++)/2;
jTextField1.setText(“ ”+t);
jTextField2.setText(“ ”+s);

Q30. Write the value of t after the execution of the following code :
int t;
int s;
s=6;
t = (8 * s++) % 7;
Worksheet 6 Netbeans
(Control Structures  If Statement)
Q1. What will be the content of jTextField1 after executing the following code:
int Num = 6;
Num = Num + 1;
if ( Num > 5)
jTextField1.setText(Integer.toString(Num));
else
TextField1.setText(Integer.toString(Num+5));

Q2. What will be the contents of jTextArea1 after executing the following statement:
jTextArea1.setText("Object\nOriented\tProgramming");

Q3. Ms. Samhita has developed a Java application through which the students of her school can
view their marks by entering their admission number. The marks are displayed in various text
fields. What should she do so that the students are able to view but not change their marks in
text fields?

Q4. The following code has some error(s). Rewrite the correct code underlining all the corrections
made:
inti, j=S;
i ==j+S;
if (i = j)
{ jTextField1 .setText("i and j are unequal");
jTextField2.setText("they are not equal"); break;
else jTextField1.setText("i and j are equal")

Q5. Write a statement to make jTextArea1 as un-editable.

Q6. Write the output of:


double x=15;
x*=3 + 5;
jTextField1.setText(“” +x);

Q7. Write Java code that takes the price of a pencil from jTextField1 and quantity of pencils from
jTextField2 and calculates total amount as price* quantity to be displayed in jTextField3 and
also find 10% tax amount to be displayed in jTextField4.
Q8. What will be displaced in of jTextFieldl after executing the following code.
int N = 20;
N = N +I;
if(N < 21)
jTextField 1.setText(Integer.toString(N+ 10) );
else
jTextFieldl.setText(Integer.toString(N+ 15));

Q9. Write Java code that takes value for a number (n) in jTextField1 and cube (n*n*n) of it to be
displayed in jTextField2.

Q10. What message will be displayed after the execution of the following code ?
int Age=64 , Relaxation= 4 ;
int ModiAge=Age-Relaxation;
if (ModiAge<60)
jOptionPane.showMessageDialog(Null, "NOT eligible") ;
else
JOptionPane.showMessageDialog(Null , "Eligible");

Q11. Write Java code that takes the cost of a pencil from jTextField1 and number of pencils from
jTextField2 and calculates total amount as cost*number to be displayed in jTextField3 and 20%
service tax out of total amount in jTextField4.

Q12. Write the purpose of each of the following statements:


(i) int x = 50;
(ii) if (x==50)
Pass=’T’;

Q13. What will be the content of jTextArea1 after executing the following code?
int Num = 4 ;
jTextArea1.setText(Integer.toString(++Num));

Q14. The following code has some error(s). Rewrite the correct code underlining all the corrections
made:
int x == Integer.parseInt(jTextField1.setText());
if x >= 18
JOptionPane.showMessageDialog(null, “You Can Vote”);
else if
JOptionPane.showMessageDialog(null, “You cannot vote”);

Q15. Write the value of z after execution of following code :


int j;
int z;
j=4;
z = (4*++j)%3;

Q16. Write statement to increase the value assigned to variable Z by 5 and then to display the
value.

Q17. What will be displayed in jTextField1 and jTextField2 after the following code is executed:
int number = 12;
if (number <15)
{
jTextField1.setText(""+ number) ;
number++;
jTextField2.setText(""+ number) ;
}
else
{
jTextField1.setText("number1”) ;
number++;
jTextField2.setText("number2”) ;
}

Q18. The following code has some error(s). Rewrite the correct code underlining all the
corrections made.
int written, interview;
written = Integer.parseInt(jTextField1.getText());
interview = Integer.parseInt(jTextField2.getText());
if (written <80) OR (interview <15)
{
System.out.println(Not selected);
}
Else;
{
System.out.println(“Selected”);
}
Worksheet 7 Netbeans
(JRadioButton & JCheckBox)
Q1. XYZ Pens Company sells each pen at the cost of < 25.00. The Programmer has developed a GUI
application as shown below :
 The number of pens bought are
entered by the user.

 The Amount is Number of Pens * 25.00

 If a person buying pens is a ‘Special


Customer’, a discount of 5% is given
on the amount, otherwise no discount
is given.

 Amount to be paid = Amount –


Discount
Write code in Java to do the following :
(i) When Calculate button is clicked the Amount, Discount and Amount to be Paid are calculated
and should be displayed in appropriate text fields.
(ii) When Clear button is clicked, all the Textfields and Checkbox are cleared.
(iii) When Stop button is clicked, the application is closed.

Q2. Ms. Sheela works in a shopping mall. To calculate net payable amount she has developed the following GUI
in NetBeans.
The shop accepts payments in three modes- Cash, Debit Card, Credit Cards. The discount given
as per mode of payment is as follows-
If the Member check box is checked then the customer gets an additional discount of 5% on net
payable amount.
I. Write the code to make the textfields for Discount( txtDiscount ) and Net Payable
(txtNetPayable) uneditable.
II. Write code to do the following-
a) When Calculate button is clicked the discount and net payable amount is calculated as per
the given criteria and displayed in discount and net payable text boxes.
b) When Clear button is clicked all the text boxes should be clear.
c) Close the application when Exit button is pressed.

Q3. Abraham is a programmer at shouryen world School. He created the following GUI in Netbeans.
The grade is calculated on the basis of percentage of total marks in five subjects (English,
Maths, Physics, Chemistry, Computers). The Grade is calculated using the following criterion :
(Each subject marks is out of 100)

Help him to write code for the following :


(i) write Java code to calculate and display Total (as sum of marks obtained in all the
subjects), percentage (as Totays) and Grade on the basis Percentage of marks and the
given criteria on the click of Command Buttons [Calculate Total], [Calculate %] and
[Calculate Grade] respectively.
(ii) Write Java code to clear all Textboxes on the click of [Clear] button.
Write Java code to close the application on the click of [exit] button.

Q4. Ms. Neelam works as a programmer in “Kidz Entertainment Zone”. She has
designed a Registration Page to calculate the total fee of summer camp depending upon the
number of activities selected by the user considering age eligibility as well. A screenshot of
the same is shown below:
Help her in writing the code to do the following:
i. After entering the age in the specified text field, when ‘Chk Eligibility’ button is clicked, a
dialogue box should be displayed with a message “Welcome” if age is in between 3-13 years
else there should be a message “Sorry! You are either underage or overage!!”. Additionally
the application should be closed if age is inappropriate.

ii. After selecting the desirable activities, total fee should be displayed in the specified text
field on the click of “Proceed” button at the rate of Rs. 1000 per activity.
iii. A discount of 20% is applicable if more than one activity is chosen by the user.
iv. After clicking on the “Net Fee” button, Net Fee should be calculated and
displayed in the respective text field as per the given formula:

Net Fee = Fee – Discount

Q5. Mr. Kunal works as a programmer in “Delicious IceCream” parlour where he has designed a
software to compute charges to be paid by the customer. A screenshot of the same is shown
below :
A customer can take any icecream out of 1 Scoop/ 2 Scoops/ IceCream Sundae.
A customer can also opt for one, two or all three toppings over IceCream.
Charges of toppings are extra. Each toppings is for 30.00. For example if two toppings are
chosen, 60.00 will be displayed as Toppings Charges.
Help Mr. Kunal in writing the code to do the following :
(i) After selecting appropriate Radio Button and checkbox(es), (if required), when ‘Calculate
Amount’ button is clicked, IceCream charges, Toppings Charges and ‘Total Amount to Pay’
should be calculated and displayed in the respective text fields.
Charges for IceCreams are as follows :

Total Amount to Pay = IceCream Charges + Toppings Charges.


(ii) When ‘CLEAR’ button is clicked, all the textfields, radio buttons and checkboxes should be
cleared.
(iii) When ‘Exit’ button is clicked, the application should close.

Q6. Ms. Angela works as a programmer in a Bus Tour Company named “Heritage Experiences”.
Groups of people come and reserve seats. There are 3 stopovers for the bus. First stop is at
Alwar, second at Jaipur, third at Udaipur. A group may choose any one destination out of Alwar,
Jaipur and Udaipur.
Ms. Angela has designed a software to compute charges to be paid by the entire group. A
screenshot of the same is shown below :
A group can opt for one destination out of Alwar / Jaipur / Udaipur. If the group is “Frequent
Traveller Group”, the group gets a 10% discount on Total charges. Help Ms. Angela in writing
the code to do the following :
(i) After selecting appropriate Radio Button and checkbox (if required), when ‘Calculate
Charges’ button is clicked, ‘Total Charges’, ‘Discount Amount’, ‘Amount to Pay’
should be calculated and displayed in the respective text fields. The Charges per
person for various destinations are as follows :

‘Total Charges’ is obtained by multiplying ‘Number of People in


Group’ with Amount per person.
If ‘Frequent Traveller Group’ checkbox is selected. ‘Discount
Amount’ is calculated as 10% of ‘Total Charges’. Otherwise
‘Discount Amount’ is 0.
‘Amount to Pay’ is calculated as :
Amount to Pay = Total Charges – Discount Amount.
(ii) When ‘CLEAR’ button is clicked, all the textfields, radio button and checkbox should
be cleared.
When ‘EXIT’ button is clicked, the application should close.

Q7. Mr. Ashish works as a programmer in “Universal Technologies Pvt. Ltd.”. He has designed a
Library software to generate the member ship fee depending upon the membership type
considering discount eligibility as well. A screenshot of the same is shown below:
Help him in writing the code to do the following:
i. After selecting appropriate Radio Button, when ‘Check Fee’ button is clicked, fee should be
displayed in the respective text field according to the following criteria:

ii. After selecting appropriate Radio Button, when ‘Check Discount’ button is clicked,
appropriate discount should be displayed in the respective text field according to the following
criteria:

iii. When ‘Calculate’ button is clicked, Net Fee should be calculated and displayed in the
respective text field as per the given formula:
Net Fee = Fee – Discount
iv. When ‘Clear All’ button is clicked, All the text fields should be cleared.

Q8. Vijay has developed a software for planning personal budget. A screenshot of the same is
shown below :
Total Income, Expenses of Bills (Water/Electricity), Groceries, Entertainment, other
expenses and whether money is to be sent to Hostel are entered by the user. Sum of
Expenses, Grand Total of Expenses and Savings are calculated and displayed by the
program.
Write the code to do the following :
(i) When ‘CALCULATE’ button is clicked, Sum of Expenses, Total Expenses and Savings
should be calculated and displayed in appropriate text fields. 3
 Sum of Expenses is calculated by adding expenses on Bills
(Water/Electricity), Groceries, Entertainment and other expenses.
 Grand Total of Expenses is calculated according to the following criteria :
If ‘Money to be sent to Hostel’ checkbox is selected, 3000.00 is to be added to the
sum of expenses. If it is not selected, Grand Total of Expenses is the same as sum of
expenses.

 Savings = Total Income – Grand Total of Expenses.

(ii) When ‘CLEAR’ button is clicked, all text fields and checkbox should be cleared. 1

(iii) When ‘CLOSE’ button is clicked, the application should close.

Q9. Mr. Pawan works as a programmer in “ABC Marketing Company” where he has designed
a Salary generator software to generate the salary of salesman in which Name and Salary are
entered by the user. A screenshot of the same is shown below:

Help him in writing the code to do the following:


i. After selecting appropriate Radio Button, when ‘Commission’ button is clicked,
commission should be displayed in the respective text field as each Salesman will get
a commission based on the units sold according to the following criteria:

ii. When ‘Gross Salary’ button is clicked, Gross Salary should be calculated and
displayed in the respective text field as per the given formula: Gross
Salary= Salary+Commission

iii . After required selection of Checkbox(es), when ‘Facility


Ch arges’ button is clicked, Facility charges will be displayed in
th e respective text field according to the following criteria:
iv. Money will be deducted from the Gross Salary according to the facilities opted by the
employee. When ‘Net Salary’ button is clicked, Net Salary should be calculated and displayed
in the respective text field as per the given formulae:
Net Salary= Gross Salary-Deductions

Q10. Seema is a junior programmer at ‘Avon Shoe Factory’. She has created the
following GUI in Netbeans.

 3 items namely Shoes, Sandals and Slippers are manufactured by the factory.
 A buyer can buy more than one item at a time.
 Each pair of shoes costs ` 1,500.00, each pair of sandals costs ` 1,000.00 and each pair of
slippers cost ` 500.00.
 The item bought will be selected by the user and the Quantity (number of pairs) bought
will be entered by the user.
 Amount to be paid for that item will be displayed in front of the item.
For example if ‘Shoe’ is selected and Quantity entered is 20, then Amount should be
displayed as 30000.
Help Seema write code for the following :
(a) When ‘Calculate’ button is clicked, the amount should be displayed in front of each item (in
the appropriate textfield) and Total amount (sum total of all the amounts) should be displayed
in the appropriate textfield. 3
(b) When Clear button is clicked, all the Textfields and Checkboxes should be cleared. 1
(c) When Stop button is clicked, the application should close.
Worksheet 8 Netbeans
(JList & JComboBox)
Q1. Glamour Garments has developed a GUI application for their company as shown below :

The company accepts payments in 3 modes- cheque , cash and credit cards.The discount given
as per mode of payment is as follows.
If the Bill Amount is more than 15000 then the customer gets an additional discount of 10% on
Bill Amount.
(i) Write the code to make the textfields for Discount (named jTextField3) and Net Amount
(named jTextField4) uneditable.
(ii) Write code to do the following:
a. When "Calculate Discount" button is clicked the discount should be calculated as per the
given criteria and it should be displayed in the discount textfield. "Calculate Net Amount"
button (named jButton2) should also be enabled.
b. When "Calculate Net Amount" button is clicked the net amount should be calculated and it
should be displayed in the net amount textfield.

Q2. A phone number, consisting of 10 digits, is stored in a string variable strPhone. Now it is
required to store this phone number in a Long type variable lngPhone. Write a Java statement
to do this.

Q3. While making a Form in Netbeans, Mr. Harihar Jha wants to display a list of countries to allow
the users to select their own country. Suggest him to choose most appropriate control out of
ListBox and ComboBox.

Q4. Item code consisting of 5 digits is stored in an integer type variable intltemCode. Mr. Srikant
wants to store this Item code in a String type variable called strltemCode. Write appropriate
Java statement(s) to help her in performing the same.
Q5. Which property of JList object is used to add values to the JList

Q6. Write statement to set the default selected value in jList1 as the third value.

Q7. Which property is to be used during design time to add a number of countries in the list box?
Worksheet 9 Netbeans
(Switch)
Q1. What is the purpose of default clause in a switch statement?

Q2. Rewrite the following program code using switch statement:


if (d == 1)
day = "Monday";
else if (d == 2)
day = "Tuesday";
else if (d == 3)
day = "Wednesday";
else
day = "-";

Q3. While working in Netbeans, Ms Kanta Surbhi wants to display 'Cleared' or 'Re-attempt required'
message depending the marks entered in jTextField. Help her to choose more appropriate
statement out of 'If statement' and 'Switch statement'.

Q4. Rewrite the following program code using a Switch statement.


if (code== 1)
Day = "Monday";
else if (code == 2)
Day = "Tuesday";
else if (code == 3)
Day= "Wednesday";
else if (code == 4)
Day = "Thursday";
else
Day = "No Match" ;

Q5. What is the purpose of break keyword while using Switch Case Statement? Illustrate with the
help of an example.
Q6. Rewrite the following program code using a If statement :
int C=jComboBoxl.getSelectedIndex() ;
switch(C)
{
case 0: Amount=Bill;break;
case 1: Amount=0.9*Bill;break;
case 2: Amount=0.8*Bill;break;
default: Amount=Bill;
}

Q7. Rewrite the following code using switch case:


int day=Integer.parseInt(jTextField1.getText());
if(day>=1 && day<=5)
jOptionPane1.showMessageDialog(this, "Working Day");
else if(day>=6 && day<=7)
jOptionPane1.showMessageDialog(this, "Off Day");
else
jOptionPane1.showMessageDialog(this, "Invalid Entry");

Q8. Mr. Sahil, a programmer in a VISA processing company, has written the following code:
int ch=Integer.parseInt(jTextField1.getText());
switch(ch)
{
case 1:
jTextField2.setText(“Short Term Single Entry Visa Granted”);
case 2:
jTextField2.setText(“Short Term Multiple Entry Visa Granted”);
case 3:
jTextField2.setText(“Long Term Single Entry Visa Granted”);
case 4:
jTextField2.setText(“Long Term Multiple Entry Visa Granted”);
default:
jTextField2.setText(“Invalid Entry”);
}
No matter what value is being entered in the text field jTextField1, this program always give a
message “Invalid Entry”. Help Mr. Sahil in identifying the problem and its reason. Also suggest
him a suitable correction in the code.

Q9. When is if-else if statement preferred over switch statement ?

Q10. Deepti works as a programmer in a travel company. She has developed the following code to
display travel detail according to user’s choice. Help her in rewriting the same code using
SWITCH CASE:

if(choice==1)
jTextField1.setText(“New Delhi to Goa”);
else if(choice==2)
jTextField1.setText(“New Delhi to Paris”);
else if(choice==3)
jTextField1.setText(“New Delhi to Bangkok”);
else
jTextField1.setText(“Pl. choose valid option”);

Q11. Rewrite the following program code using IF ELSE IF instead of SWITCH
statement.
String rem;
int code = Integer.parseInt(jTextField1.getText());
Switch (code)
{
case 1 : rem = “Classes start on 8th April”;
break;
case 2 : rem = “Classes start on 10th April”;
break;
case 3 : rem = “Classes start on 12th April”;
break;
default : rem = “Contact Admin Office”;
}
Worksheet 10 Netbeans
Loops
Q1. How many times will each of the following loops execute? Which one of these is an entry
control loop and which one is an exit control loop?

Q2. The following code has some error(s). Rewrite the correct code underlining all the corrections
made:
int i=2; j=5;
while j>i
{ jTextField1.getText("j is greater");
j--;++i;}
JOptionPane.showMessageDialog("Hello");

Q3. What will be the values of x and y after execution of the following code:
int x, y=0;
for (x = 1; x<=5; ++x)
y = x++;
--y;

Q4. Rewrite the following program code using a for loop:


int i = 1, sum = 0;
while (i<1 0)
{ sum+= i;
i +=2;
}

Q5. What will be the value of A and B after execution of the following code :
int A= 100, B;
for (B = 10; B < = 12; B+ +)
{
A+=B;
}
JOptionPane.showMessageDialog (this, "A: "+A+" B: "+B+"") ;

Q6. The following code has some error(s). Rewrite the correct code underlining all the corrections
made :
Int P = 3;sum = 0;//Declaring P and sum as Integer
{
sum=P;
P+=3;
}
while (P=<l2)
jTextField1(Integer.tostring(sum));

Q7. The following code has some error(s). Rewrite the correct code underlining all the corrections
made;
int Total=O, Jurnp=5;
intI;
for (i = 0, I=< 5, i++)
{
Jump+=5,
Total+=Jump;
}
jTextAreal.showText (""+Total);

Q8. What will be the content of the jTextArea1 after executing the following code (Assuming that
the jTextArea1 had no content before executing this code)?
for (int C=1 ; C=4; C++)
{
jTextAreal.setText(jTextAreal.getText() + " " + Integer.toString{C*C));
}

Q9. How many times does the following while loop get executed ?
int K=5;
i nt L=36;
while (K<=L)
{
K+=6 ;
}

Q10. What will be the values of variables 'm' and 'n' after the execution of the following code ?
int P , Q=O ;
for(P=1 ;P<=4;P++)
{
Q+=P;
Q--;
}

Q11. Write the output of the following code in Java:


int x = 0;
while(x<=1)
{
Systemt.out.println(“x”);
x = x + 1;
}

Q12. How many times does the following loop execut ?


int x;
x = 4;
do
{
x = x – 1;
System.out.println(x);
}while (x>=2);

Q13. How many times the following loops will execute the statements inside it ?
Which one of them is Entry Control and which one is Exit Control ?

Q14. What will be displayed in jTextField1 and jTextField2 after the execution of the following
loop ?
int Total=0,End=10;
for (int Turn=l;Turn<=End;Turn+=2)
Total++;
jTextFieldl.setText (Integer.toString(Total )) ;
jTextField2.setText(Integer.toString(Turn));

Q15. Rewrite the following programme code using a while loop statement :
int Last=Integer.parseint(jTextFieldl .getText()) ;
for(int C=l;C<=Last;C++)
jTextAreal.setText{Integer.toString(C));
Q16. Observe the following code carefully and find which statement will never get executed in the
code.
int Count=1; //Statement 1
do //Statement 2
{ //Statement 3
if (Count<15 ) //Statement 4
jTextFieldl.setText("Jump"); //Statement 5
else //Statement 6
jTextFieldl.setText("Stop" ); //Statement 7
Count+=4; //Statement 8
} //Statement 9
while (t<=15 ) ; //Statement 10

Q17. What will be the final value of variable x after the following code is executed:

int x=10;
while(x>1)
{
x=x/3;
++x;
}

Q18. Rewrite the following code using for loop:

int attempt=0;
while(attempt<=3)
{
String login=jTextField1.getText();
String pwd=jTextField2.getText();
if(login.equals("XII") && pwd.equals("IP"))
{
jOptionPane1.showMessageDialog(null, "Welcome");
break;
}
else
jOptionPane1.showMessageDialog(null, "Pl try again");
attempt++;
}

Q19. The following code has error(s). Rewrite the correct code underlining all the
corrections made :
int start=2;end=20;
do;
{
start=start+start;
while(start<=end)

Q20. What values will be assigned to b, r and t after execution of the following code :
for(int i = 0; i <= 2; i++) {
switch(i) {
case 1:
{
b=i;
break;
}
case 2:
{
r=i;
break;
}
default:{
t=i; }
}
}

Q21. What will be displayed in jTextField1 after the following code is executed ?
int i, p, n;
n=3;
p=1;
if(n < 0)
jTextField1.setText(“Not Valid”);
else
{
for(i=1; i<=n; ++i)
{
p = p* i;
}
}
jTextField1.setText(“ ”+p);

Q22. Write the value that will be stored in variable p after execution of following code. How many
times will the loop execute ?
int y, x, p;
y = 3;
x = 5;
p = 0;
do
{
p = p + x;
y = y – 1;
}
while (y > 0);

Q23. The following code has error(s). Rewrite the correct code underlining all the corrections made :
while num > 0;
{
remainder assigned = num % 10;
reverse = reverse * 10 + remainder;
num / 10 = num;
}

Q24. What will be displayed in jTextArea1 after the following code is executed:
int i;
for(i=10;i<50;i=i+30)
{
i=i+15;
jTextArea1.append(""+i);
}
jTextArea1.append("\n"+i);

Q25. Rewrite the following code using do while loop:


int num=Integer.parseInt(jTextField1.getText());
while(num<=40)
{
jOptionPane1.showMessageDialog(null,num);
num=num*20;
}
jOptionPane1.showMessageDialog(null,"Bye Bye");

Q26. The following code has error(s). Rewrite the correct code underlining all the
corrections made :
value1=1, value2=2;
while(value1<value2)
jTextArea1.append ("\n"+value1*value2;
value1++
}

Q27. Rewrite the following code using WHILE loop :


int sum = 0;
for(int i=9;i>=1;i--)
{
if(i%3==0)
sum=sum + i;
else
sum=sum – i;
}

Q28. The following code has error(s). Rewrite the correct code underlining all the
corrections made :
int x=10;
int y=50;
do;
{

x+5=x;
y–5=y;
while(x<=y);

Q29. How many times will the loop execute?

int value1 =7,value2=19;


do
{
JOptionPane.showMessageDialog(null,value1+value2);
value1=value1+2;
value2=value2-2;
}while(value1<=value2);

Q30. Rewrite the following code using WHILE loop:


int x=100;
for(int i=2;i<=22;i=i+4)
{
jTextArea1.append("\n"+(i+x));
x=x-2;
}

Q31. The following code has error(s). Rewrite the correct code underlining all the corrections
made :
int n=5,int i=1,f=1;
do;
{ f=f*i; i++;
while(i<=n)
jTextField1.setText(""+f);

Q32. Write the output that will be generated by the code given below :
int i;
int t;
for (i = 5; i <=10; i = i+5)
{
t = i+3;
system.out.println(“ ”+t);
}

Q33. How many times will the following loop execute :


int z = 7, sum = 0;
do
{
sum = sum + z;
z = z+2;
system.out.println(“ ”+z);
}
while (z <=12);
Worksheet 11 Netbeans
Functions
Q1. What will be the contents of jTextField1 and jTextField2 after executing the following code:
String s = "ABC Micro Systems";
jTextField1.setText(s.length()+" ");
jTextField2.setText(s.toLowerCase());

Q2. What is the purpose of break statement in a loop?

Q3. Write the purpose of the following statement:


jTextField1.setText("lnformatics".substring(3));

Q4. What will be the contents of jTextField1 and jTextField2 after executing the following code:
jTextField1.setText(Math.round(2.3)+"");
jTextField2.setText(Math.pow(2,3)+"");

Q5. Given a string object namely 'VALUE', having value as "324" stored in it. What will be result of
the following :
JOptionPane.showMessageDialoge(null, " "+
(Value.length() + Integer.parseint(value)));

Q6. Given a string object named Pay having value as "68000" stored in it. Obtain the output of the
following :
JOptionPane.showMessageDialog(null, “ ” +Salary .length() +Integer.parseInt (Salary)) ;

Q7. List two purposes of ‘+’ operator in Java.

Q8. What will be the contents of jTextField1 and jTextField2 after executing the following code?
String s= “ClassTwelve”;
jTextField1.setText(s.length()+ “ “);
jTextField2.setText(s.toUpperCase());

Q9. What will be the content of jTextArea1 and TextField1 after the execution of the following
statements ?
(i) jTextAreal. setText ("Go\tGreen\niNDIA");
(ii ) String Message="All The Best";
jTextFieldl.setText((Message.length()-6) +"") ;

Q10. What will be the contents of Text1 and Text2 after the following code is executed ?
String Text2 , Text1;
Textl= "Good Morning" ;
Text2= "India";
Textl=Text2.concat(Textl ) ;
Q11. What is a button group ? Which control is generally used with a button group?

Q12. Which property of ListBox is used to display values in the list?

Q13. What will be the value X1 after the execution of the following code ?
String X1= "Graduate" ,X2="Post" ;
X1=X2 .concat(X1) ;

Q14. Give the ouput of the following java code:


String name=”Sid Nayar”;
int T=name.length(),N;
N=150-T;
jTextField2.setText(Integer.toString(T));
jTextField3.setText(Integer.toString(N));

Q15. What will be the content of the jTextArea1 after executing the following code :
INT n = 89;
jTextAreal.setText(Integer.toString(++n));

Q16. Write the output :


System.out.println(Math.pow(4.0,2.0));
System.out.println(Math.round(6.459));

Q17. What will be the content of jTextField1 and jTextField2 after executing the following code:
String st=”New to Information Technology”;
jTextField1.setText(st.replace(“Technology”,”Practices”);
jTextField2.setText(st.substring(7));

Q18. i. Find the output of the following Java code snippet after execution of each java statement
labelled as Line 1, Line 2, Line 3, Line 4:

String userid="INDIA",pwd="";
pwd=userid.substring(0,2); //Line 1
int l=userid.length(); //Line 2
pwd=pwd.toLowerCase(); //Line 3
pwd=pwd.concat(""+l); //Line 4

Q19. Write the values that will be assigned to a, b, c and m after executing the following Java code :
String p, q, r, a, b, c;
int m;
p = “AISSE2017”;
q = “Learning Java”;
r = “ ProgrammingFun ”;
a = p.substring(3,6);
b = q.concat(p);
c = r.trim();
m = c.length();

Q20. Find the output of the following Java code snippet after execution of each java statement
labelled as Stmt 1, Stmt 2, Stmt 3, Stmt 4:
String str1="VASUDHAIVA",str2="KUTUMBAKAM";
jTextArea1.append((str1.substring(0,4))); //Stmt 1
jTextArea1.append((str2.substring(4))); //Stmt 2
jTextArea1.setText(jTextArea1.getText()+jTextArea1.getText().length());//Stmt 3
jOptionPane1.showMessageDialog(null,jTextArea1.getText().length()); //Stmt 4

Q21. Write the contents of jTextField1, jTextField2, jTextField3 and jTextField4 when the
following statements are executed :
String x;
String str = “Java”;
x = str.concat (“study”);
double a = 7.8765;
jTextField1.setText (x.length()+“ ”);
jTextField2.setText (x.toUpperCase () );
jTextField3.setText (x.substring (2,5));
jTextField4.setText (Math.round(7.8765)+“ ”);

Q22. String City="Delhi",PinCode="110001",Str=""; City=City+" "+PinCode;


JOptionPane.showMessageDialog(null,City);
int l=City.length();
int i=0;
while(i<l)
{
Str=Str+City.substring(6);
i=i+15;
} JOptionPane.showMessageDialog(null,Str);

Q23. What will be the contents of jTextField1 and jTextField2 after executing the
following code :
String s = “Best”;
String r = “Luck”;
String z;
Z = r.concat(s);
jTextField1.setText(z);
jTextField2.setText(r.toUpperCase());
Worksheet 12 Netbeans
(Advanced Programming Concepts)
Q1. Define a class with reference to object oriented programming.

Q2. Define inheritance with reference to object oriented programming.

Q3. Define data encapsulation with reference to Object Oriented Programming.

Q4. Name the methods used to convert one type of data to another in the following statements of
Java.
int Num=Integer.parseInt(jTextFieldl.getText());
jTextField2.setText(Integer.toString(Num));

Q5. Distinguish between '/' and ‘%’ operators

Q6. Write one difference between IF statement and SWITCH statement.

1. Which of the following is not a feature of Object Oriented Programming?


a. Inheritance b. Data Overloading
c. Polymorphism d. Objects
2. Identify which of the following are not valid examples of method overloading?
a. round() b. subString()
c. subStr() d. getText()
3. Which of the following statements about the Java language is true?
a. Both procedural and OOP are supported in Java
b. Java supports only procedural approach
c. Java supports only OOP approach
d. None of the above
4. Which of the following is an Abstract class?
a. JTextArea b. String c. Math d. JFrame
5. Which of the following statements is false about objects?
a. Object is an instance of a class
b. Object is capable of storing data
c. Each object has its own copy of methods
d. None of the above
6. A class can have many methods with the same name as long as the number of
parameters or type of parameters is different. This OOP concept is known as
a. Method Overriding b. Method Overloading
c. Method Invocating d. Method Labelling
7. Which of the following statement is true?
a. A super class is a subset of a sub class
b. class Second extends First means First is a sub class
c. class Second extends First means Second is a super class
d. None of the above
8. Which feature(s) of Object Oriented Programming is illustrated in the following code
snippet:
String A = "Hello", B = "There", C;
String C = A + B;
int num1 = 10, num2 = 20;
int num3 = num1 + num2;
C = B.substring(1);
B = A.substring(3);
a. Method Overloading b. Inheritance
c. Operator Overloading d. None of the above

1. Define the term Polymorphism. What are the two ways polymorphism is
demonstrated in Java?
2. What is the importance of abstract classes in programming?
3. Write a short note on Operator overloading and Method Overloading citing examples
for both.
4. Carefully study the code given below. It is giving an error whenever it is compiled:
float f = 12.5;
double d = 123.6543;
int num1 = Math.round(f); //Statement 1
float num2 = Math.round(d); //Statement 2
int num2 = Math.round(d); // Statement 3
Identify the statement that will result in an error. Justify.
5. Carefully study the code given below:
String Message = "Hello! How are you?", Msg1, Msg2;
Msg1 = Message.substring(7);
Msg2 = Message.substring(0,5);
What will be the contents of the variables Msg1 and Msg2 after the above statements
are executed?
6. Study the following code and answer the questions that follow:
String SMS=jTextArea1.getText();
int L=SMS.length(),Balance;
Balance=160-L;
jTextField2.setText(Integer.toString(L));
jTextField3.setText(Integer.toString(Balance));
a. Name any one native class of Java used in the above code.
b. Name the object created of the above mentioned native class.
c. Identify and name two methods of the native class.
d. Name the method used to convert one type of data to another and also mention the
data type before and after conversion.
7. Study the following code and answer the questions that follow:
public class Example extends javax.swing.JFrame
{
/** Creates new form named Example */
public Example()
{
initComponents();
}
}
a. Which feature of object oriented programming is depicted above?
b. Name the base class and the derived class.
c. Name the keyword used for passing on characteristics of the base class to
derived class.
8. What is procedural programming paradigm?
9. What is Object Oriented Programming paradigm?
10. Differentiate between procedural programming and OOP.
11. What is Data Encapsulation?
12. Name language which support both Procedural as well as OOP
13. Java supports only _________________
14. ________________ follows bottom up approach in program design and emphasizes on
safety and security of data.
15. What is inheritance?
16. What is class?
17. What are the three visibility modes of data members in a class?
18. Usually, data members of a class are kept in ________ or _________ visibility modes
and methods are kept in the ____________ visibility mode.
19. What is an Object?
20. Class and objects are related to each other in the same way as __________ and
_____________.
21. JTextField, JLabel, JTextArea, JButton, JCheckBox and JRadioButton are all
______________ and the jTextField1, jLabel1, jTextArea1, jButton1,
jCheckBox1 and jRadioButton1 components are all ______________.
22. Two examples of polymorphism are ____________ and _______________.
23. What is method overloading?
24. The _________ method of the Math class and the _____________ method of the String
class are good examples of method overloading.
25. A very good example of operator overloading is the ________ operator.
26. What are concrete classes?
27. What are abstract classes?
28. _______________ are normally used as base class in inheritance for
which no direct object is required to be created.
29. What is the most important aspect or advantage of inheritance?
30. javax.swing.JFrame acts as a __________ class and the name given by you for the
JFrame Form acts as the name for the ________________.
31. ____________ keyword is used to inherit data members and methods of
the base class.

You might also like