CBSE QuestionJava Questions
CBSE 2016
a)Identify the odd one out of the following statements. State reason for your choice.[1]
(i)switch
(ii)do while
(iii)while
(iv)for
b)What is the difference between setVisible() and setEnabled() method ?[1]
c)What is the difference between the following statements (i) and (ii).[1]
(i)a=5;
(ii)if(a==5)
x=3;
d)Write the output in jTextField1 in depcode is 3.[1]
switch(depcode)
{
case 1 :
allowance=4000;
break;
case 2 :
allowance=3200;
break;
default:
allowance=1000;
}
jTextField1.setText(“”+allowance);
e)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. [2]
f)When is if-else if statement preferred over switch statement?[1]
g)What is the purpose of break statement?[1]
h)What will be displayed in jTextField1 and jTextField2 after the following code is executed: [2]
int t;int s;
s = 2;
t = (4 * s++) / 2;
jTextField1.setText(“ “+t);
jTextField2.setText(“ “+s);
i)Write the content of jTextField1, jTextField2, jTextField3 and jTextField4 when the following statements are executed : [2]
String x;
String str = “Java”;
x = str.concat ( “study”);
double a = 7.8765;
jTextField1.setText(x.length()+” “);
jTextField1.setText(x.toUpperCase()+” “);
jTextField1.setText(x.substring(2, 5));
jTextField1.setText(Math.round(7.8756)+” “);
j)Rewrite the following code using WHILE loop:[2]
int sum = 0;
for(int i=9; i>=1; i--)
{
if(i%3==0)
sum = sum + i;
else
sum = sum – i;
}
k)The following code has error(s). Rewrite the correct code underlining all the corrections made: [2]
int x=10;
int y=50;
do;
{
x+5 = x;
y-5 = y;
while(x<=y);
l)Vijay has developedsoftware 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, Grant Total of Expenses and Savings are calculated and displayed by the program.
(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 sends 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 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.[1]
m)Ms. Arora is creating a form for accepting Visa applications. Help her to choose most appropriate controls out of ListBox, ComboBox, TextField, TextArea, RadioButton, CheckBox, Label and CommandButton for the following enteries: [2]
S. No. / Function1. / To enter EMAIL ID
2. / To choose GENDER
3. / To enter NATIONALITY from countries given as options.
4. / To enter REMARKS in the form of a paragraph about the purpose of visit.
CBSE 2015
1.
a)Write the value of t after the execution of the following code:[1]
int t;
int s;
s = 6;
t = (8 * s++) % 7;
b)In s SWITCH statement, what is the purpose of BREAK statements?[1]
c)Write Java code to assign the 70 to variable y. Then decrease the value of y by 5 and store it in variable z. [2]
d)Write the output that will be generated by the code given below:[2]
int i;
int t;
for(i = 5; i <= 10 ; i = i + 5)
{
t = i + 3;
System.out.println(“ “+t);
}
e)The following code has some error(s). Rewrite the correct code underlining all the corrections made. [2]
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”);
}
f)How many times will the following loop execute:[2]
int z = 7, sum = 0;
do
{
sum = sum + z;
z = z + 2;
System.out.println(“ “+z);
}
while ( z <= 12);
g)Rewrite the following program code using IF ELSE IF instead of SWITCH statement.[2]
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”;
}
h)Write the values of sum and t after execution of the following code:[2]
int sum, t;
sum = 27;
t = 3;
sum = sum + 2 * (++t);
i)What will be the contents of jTextField1 and jTextField2 after executing the following code: [2]
String s = “Best”;
String r = “Luck”;
String z;
z = r.concat(s);
jTextField1.setText(z);
jTextField2.setText(r.toUpperCase());
j)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 Rs. 1,500.00, each pair of sandals costs Rs. 1,000.00 and each pair of slippers cost Rs. 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 oe 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.[1]
j)James works for a Garments company. He has created a form for the employees. Help him choose most appropriate controls from ListBox, ComboBox, TextField, TextArea, RadioButton, Checkbox, Label and Command Button for the following enteries : [2]
S. No. / Function1. / To enter first name of employee
2. / To select gender (M/F)
3. / To choose category of employee (Permanent/Temporary)
4. / To allow entering remarks about the employee in the form of paragraph.
CBSE 2014
1.)
a)Distinguish between ‘/’ and ‘%’ operators.[1]
b)What is a button group? Which control is generally used with a button group?[1]
c)Which property of ListBox is used to display values in the list?[1]
d)What will be the values of variables sum and sum1 after the execution of the following loops? [2]
Loop Aint v=6, sum=0;
while (v>3)
{
sum += v;
v -= 2;
} / Loop B
int w=6, sum1=0;
do
{
sum1 += w;
w -= 2;
} while (w>3);
e)What will be displayed in jTextArea1 after the execution of the following loop?[2]
for( int I=5; I<=25; I+=5)
jTextArea1.setText(jTextArea1.getText()+” “+Integer.toString(2*I));
f)Name methods used to perform the following in NetBeans:[2]
(i)To set component visible at run time
(ii)To return the index value of the selected item from the list.
g)Define the term ‘Inheritance’.[2]
h)Rewrite the following program code using Switch Case statement:[2]
int choice=Integer.parseInt(jTextField1.getText());
if(choice==1)
jTextField2.setText(“January”);
else if(choice==2)
jTextField2.setText(“February”);
else if(choice==3)
jTextField2.setText(“March”);
else if(choice==4)
jTextField2.setText(“April”);
else if(choice==5)
jTextField2.setText(“May”);
else
jTextField2.setText(“Only 1 to 5 please”);
i)What will be the value X1 after the execution of the following code?[1]
String X1 = ”Graduate”, X2 = ”Post”;
X1 = X2.concat(X1);
j)Write Java statement to make a jTextField1 uneditable during execution.[1]
k)What will be displayed in jTextArea1 after the execution of the following code?[2]
int Z = 4;
do
{
jTextArea1.setText(Integer.toString(++Z));
Z = Z + 1;
} while (Z<=8);
l)Give the output of the following java code:
String name = “Sid Nayar”;
int T = name.length(), N;
N = 150 – T;
jTextField1.setText(Integer.toString(T));
jTextField2.setText(Integer.toString(N));
m)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)
Percentage / Grade>= 90 / A
>= 70 & < 90 / B
>= 50 & < 70 / C
>= 33 & < 50 / D
< 33 / E
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 (a Total/5) 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.
(iii)Write Java code to close the application on the click of [exit] button.
n)Steeve Antony works for HM Academy. He wants to create controls on a form for the following operations. Choose appropriate controls from Text box, Label, Radio Button, List box, Combo box, Check box and Command button. [2]
S. No. / Operation1. / Enter Name of Employee
2. / Select Department from a list of department names
3. / Gender out of option M and F
4. / Submit Form
CBSE 2013
1.)
a)Name method is used to extract value of Index while using List Box in Java.[1]
b)What is the difference between the use of isSelected and setSelected methods used with JCheckBox in Java? [1]
c)Name any two commonly used method of JComboBox.[1]
d)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? [2]
Loop 1.int J = 8, total = 0;
while (J > 1)
{
total += j;
j -= 2;
} / Loop 2.
int J = 8, total = 0;
do
{
total += j;
j -= 2;
}
while (J > 1);
e)What will be displayed in jTextField1 and jTextField2 after the execution of the following loop? [2]
int Total=0, End=10;
for(int Turn=1; Turn<=End; Turn+=2)
Total++;
jTextField1.setText (Integer.toString(Total));
jTextField2.setText (Integer.toString(Turn));
f)Name the methods used to convert one type of data to another in the following statements of Java. [2]
int Num = Integer.parseInt(jTextField1.getText());
jTextField2.setText(Integer.toString(Num));
g)What will be the content of jTextArea1 and jTextField1 after the execution of the following statements? [2]
(i)jTextArea1.setText(“Go\tGreen\nINDIA”);
(ii)String Message=”All The Best”;
jTextField1.setText((Message.length()-6)+” “);
h)Rewrite the following program code using a while loop statement:[2]
int Last=Integer.parseInt(jTextField1.getText());
for ( int C=1; C<=Last; C++)
jTextArea1.setText(Integer.toString(C));
i)Observe the following code carefully and find which statement will never get executed in the code. [1]
int Count=1;// Statement 1
do// Statement 2
{// Statement 3
if(Count<15)// Statement 4
jTextField1.setText(“Jump”);// Statement 5
else// Statement 6
jTextField1.setText(“Stop”);// Statement 7
Count += 4;// Statement 8
}// Statement 9
while( Count<=15);// Statement 10
j)Write Java statement to make jButton1 disabled.[1]
k)What will be displayed in jTextField1 and jTextField2 after the execution of the following code? [2]
int Sum, One=3, Two=5;
Sum = One + Two++;
jTextField1.setText(Integer.toString(Sum));
jTextField2.setText(Integer.toString(Two));
l)What will be the contents of Text1 and Text2 after the following code is executed?[2]
String Text1, Text2;
Text1 = “Good Morning”;
Text2 = “India”;
Text1 = Text2.concat(Text1);
m)Shekhar is a junior programmer at Ducom Enterprises. He created the following GUI in Netbeans.
Help him to write code for the following:
(i)To calculate Income Tax to be paid and display in jTextField4 on the click of Command Button “Calculate Income Tax” as per the following condition: [2]
If the Basic is less than 50000 the Income Tax = Basic * 0.2
And if is greater or equal to 50000 then Income Tax=Basic * 0.3
(ii)To calculate Salary and display in jTextField5 on the click of Command Button “Calculate Salary”. [2]
Hint: Salary = (Basic + Dearness Allowance + House Rent Allowance) – Income Tax/
(iii)To Clear all TextFields on the click of Command button “Clear”.[1]
n)Shobhit is creating a form for his company. Help her to choose most appropriate controls from ListBox, Combo box, TextField, TextArea, RadioButton, CheckBox, Label and Command button for the following enteries [2]
S. No. / Function1. / To select citizenship
2. / To allow to input grade out of ‘A’ to ‘D’
3. / To allow selecting one or more food items
4. / To allow entering Feedback in the form of a paragraph
By Kanhaya Sir Mobile No.: 9868772318Page 1 of 10