GRADE 11th JAVA CONCEPTS
GRADE 11th JAVA CONCEPTS
Grade: XI
Subject: Information Technology (802)
Topic: Introduction to Java Programming
In Java, the GUI programming is done through Swing API ( Application Programming Interface).
Swing is a set of classes that provides more powerful and flexible components than are possible with AWT
(Abstract Windows Toolkit). It supplies buttons, chekboxes, labels, tabbed panes, scrool pans, trees, tables, dialog
boxes etc.
The swing controls/components are categorized as:
Components: Self contained graphic entity ( eg. JLabel, JBotten, JTextField etc)
Containers: Component that can hold other components (eg. JPanel, JFrame, JDialog, JWindow)
COMMONLY AVAILABLE SWING CONTROLS IN JAVA
jFrame: A Frame is a container control, in which all the controls can be lace.
jLabel: JLable allows placing un-editable text on the Frame/Panel
jTextField: JTextFeild allows placing editable text on the Frame/Pane. User can enter text in a text field
during runtime.
jbutton: is used to initiate an action when it is clicked.
jList: is a group of values or items from which one or more selections can be made.
jComboBox: jComboBox is similar to jList but also allow to enter editable text during run time. It is a
combination of jTextFiled and jList.
jRadioButton: Allow us to choose a single item from a group of jRadioButton options.
jCheckBox: Allow us to choose one or more items from a group of jCheckBox options.
jPasswordField: Allow us to enter a text during the run time but shows an encrypted text instead of the
original text
jTextArea: JTextArea is a multi-line text component to enter or edit text.
Focus: The control under execution is said to have the focus. The control having the focus obtains input
form the user.
getText(): getText() method is used to obtain the text from a jTextFeild during the run time.
setText(): setText() method is used to set or change the text of a jTextFeild during run time.
Page 1 of 12
Learning.concat("Java")
Ans: Error
Page 2 of 12
22. Which events gets fired when a user click a JButton and JRadioButton.
Ans: ActionPerformed
23.Which of the following is a selection construct?
a. do while Loop b. for Loop c. while Loop d. None of these
Ans: d . None of these
24. What will be used if there are two or more possible options?
Ans: We can use if…..else conditional statement or switch……case statement.
25.Name the loop that never ends.
Ans: Infinite loop. For example:
for( k=1;k<=10;k++)
{
System.out.println(“It is infinite loop”); k=k- 1;
}
26.Which braces is used to enclose statements in a block statement.
Ans: { } Curly braces
27. Which of the following is an exit controlled loop?
a. for loop b. do while Loop c. while loop d. none of these
Ans: do…. while loop
28.Which process is used to translate a task into a series of commands that a computer will use to
perform that task?
Ans: Project design
29. Which of the following component is the best suited to accept the country of the user?
A. List B Combo box C Radio button D Check box
Ans: List and combo box
30. Which construct will be used to find the sum of the first 10 natural numbers?
Ans: for loop
31. Which of the following is not a good programming guideline?
Ans: Using text fields to accept input of marital status
21.Name the primitives datatypes in java.
Ans: Numeric type , Fractional type, Character type and Boolean type.
22.Which events gets fired when a user click a JButton and JRadioButton.
Ans: ActionPerformed
23.Which of the following is a selection construct?
a. do while Loop b. for Loop c. while Loop d. None of these
Ans: d . None of these
24.What will be used if there are two or more possible options?
Ans: We can use if…..else conditional statement or switch……case statement.
25.Name the loop that never ends.
Ans: Infinite loop. For example:
for( k=1;k<=10;k++)
{
System.out.println(“It is infinite loop”); k=k- 1;
}
26.Which braces is used to enclose statements in a block statement?
Ans: { } Curly braces
27.Which of the following is an exit controlled loop?
Page 3 of 12
a. for loop b. do while Loop c. while loop d. none of these
Ans: do…. while loop
28.Which process is used to translate a task into a series of commands that a computer will use to
perform that task?
Ans: Project design
29. Which of the following component is the best suited to accept the country of the user?
A. List B Combo box C Radio button D Check box
Ans: List and combo box
30. Which construct will be used to find the sum of the first 10 natural numbers?
Ans: for loop
31. Which of the following is not a good programming guideline?
Ans: Using text fields to accept input of marital status
Page 4 of 12
//Statements
}while (i> 20);
Ans: 1 time
7. What will be the contents of jTextield1 and jTextField2 after executing the following statement:
StringBuffer s= new StringBuffer(“Common Wealth”);
int c= s.capacity();
s.insert(0,’E’); s.reverse(); jTextField1.setText(“”+c);
jTextField2.setText(s.toString());
Ans: 29
htlaeWnommoCE
8. Find the output of the following code snippet:
int First = 7;
int Second = 73; First++;
if (First+Second> 90)
jlabel1.setText("value is 90 ");
else
jlabel1.setText("value is not 90 ");
Ans: value is not 90
9. Find the output
int Number1 = 7, Number2=8;
int Second = 73;
if (Number1>0 || Number2>5)
if (Number1>7)
jTextField1.setText("Code Worked");
else
jTextField1.setText("Code MightWork");
else
jTextField1.setText("Code will not Work");
Ans: Code MightWork
10. How many times will the following loop get executed?
x = 5;
y = 36;
while ( x <= y) { x+=6; }
Ans: 6
11. What will be the content of the jTextArea1 after executing the following code?
Int Num = 1;
do{
jTextArea1.setText(Integer.toString(++Num) + " ");
Num = Num + 1;
}
while(Num<=10)
Ans: 10
12. What will be the contents of jTextfield1 and jTextfield2 after executing the following code:
String s=”KENDRIYA VIDYALAYA GUNA”
jtextfield1.setText(s.length()+” “);
jtextfield2.setText(Math.round(2.34)+“”);
Page 5 of 12
Ans: 23 2
13. What will be the value of s after executing the following code?
double i,sum=2
for(i=3;i<8;++i)
{
if(i%4= =0)
{
break;
sum=Math.pow(sum,i);
}
else
sum+=i/2;
}
Ans: 150.0625
14. 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));
Ans: New to Information Practices
Information Technology
18. Predict the output for tan & tan1 if sac equals 7?
int tan = 0, tan1 = 4 ;
if ( sac == 2 )
{
tan = 4 ; tan1 = 0;
}
else
if (sac == 8)
{ tan = 0 ; tan1 = 4; }
JOptionPane.showMessageDialog( null , “ tan = “ + tan +” , tan1 = “ + tan1 ) ;
Ans: tan = 0 tan1=4
19. Give the output for the following code fragment:
v = 20;
do
{
JOptionPane.showMessageDialog( null , v + “ ” ) ;
}while ( v< 50 ) ; JOptionPane.showMessageDialog( null ,“Bye“) ;
Ans: Infinite loop
20. What will be the output produced by following code fragment?
flaot x=9;
float y=5;
int z=(int)(x/y);
switch(z)
case 1:
x=x+2;
case 2:
Page 6 of 12
x=x+3;
default:
x =x+1;
}
System.out.println(“value of x:”+x);
Ans: 15
24. What will be the contents of jTextField1 and jTextField2 after executing the following code:
String s = “Sun Micro Systems”;
jTextField1.setText(s.length()+””);
jTextField2.setText(s.toLowerCase());
Ans: jTextField1 : 17 jTextField2 : abc micro systems
25 What values will be assigned to the variable ua ,ub, uc and fail after execution of the following
program segment:
int i=0,ua=0,ub=0,uc=0,fail=0;
while(i<=5)
{
switch ( i++ )
{
case 1 :
++ua;
case 2 :
++ub;
uc++;
break;
case 3 :
case 4 :
++uc;
ua++;
ub++;
break;
default :
++fail;
}}
Ans: ua=1 ub=1 uc=0
Page 7 of 12
while( j>i)
{
jTextField1.getText(“j is greater”);
j--; ++i;
}
JOptionPane.showMessageDialog(“Hello”);
2. Rewrite the code after removing the errors (if any) and underline the corrections.
int sum;value;inct;
int i
for(i= =0;i=10;i++)
sum=sum+i;
inct++;
Ans:
int sum,value,inct;
int i;
for(i=0;i<=10;i++)
sum=sum+i;
inct++;
3. Rewrite the code after removing the errors (if any) and underline the corrections.
int y=3;
switch(y);
{
case 1:
System.out.print(“Yes its One”);
case2:
System.out.println(“Yes its more than Two”);
break;
case else:
System.out.print(“Invalid Number):
Ans:
int y=3;
switch(y)
{
case 1:
System.out.print(“Yes its One”);
break;
case 2:
System.out.println(“Yes its more than Two”);
break;
default:System.out.print(“Invalid Number):
}
4. Rewrite the code after removing the errors (if any).
int x = = 0;
int n= Integer.parseInt(Jlabel1.getText);
Page 8 of 12
Ans:
int x = 0;
int n= Integer.parseInt(JLabel1.getText());
5. Rewrite the code after removing the errors (if any). M=1, N=0;
For(;m+n<19;++n) System.out.println(“hello”);
M=m+10;
Ans:
m=1; n=0;
for(;m+n<19;++n) System.out.println(“hello”);
m=m+10;6.
Rewrite the code after removing the errors (if any).
int y=6,p;
do{
y=3.14*y;
p=y%10;
if p=2 System.out.print(“Two”);
while(y>1)
Ans:
int y=6,p;
do
{
y=3.14*y;
p=y%10;
if (p==2)
System.out.print(“Two”);
}while(y>1);
7. Rewrite the following program code using a for loop:
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;
}
8. Rewrite the following code using while loop :
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;++j)
{
System.out.print(j);
}
Page 9 of 12
System.out.println( );
}
Ans:
int i=1,j;
while(i<=4)
{
j=1;
while(j<=i)
{
System.out.print(j);
++j;
}
i++;
System.out.println();
}
9. Rewrite the following code using
while loop
int i,j;
for(i=1,j=2;i<=6;i++,j+=2)
System.out.println(i++);
System.out.println(“Finished!!!”);
Ans:
int i=1,j=2;
while(i<=6)
{
System.out.println(i++);
i++; j+=2;
}
System.out.println(“Finished!!!”);
10. Rewrite the following code using for loop.
int i=0; while(++i<20)
{
if( i==8)
break;
System.out.println(i++);
Ans:
}
int i;
for(i=1;i<20;++i)
{
if( i= =8)
break;
System.out.println (i++);
}
11. Write the equivalent switch case for the following code:
if (num1 = =1 )
Page 10 of 12
jTextField1.setText(“Number is one”);
else If (num1 = =2 )
jTextField1.setText(“Number is two”);
else If (num1 = =3 )
jTextField1.setText(“Number is three”);
else
jTextField1.setText(“Number is more than three”);
Ans: Switch(num1)
{
case 1 :
jTextField1.setText(“Number is one”);
break;
case 2 :
jTextField1.setText(“Number is two”);
break;
case 3 :
jTextField1.setText(“Number is three”);
break;
default:
jTextField1.setText(“Number is more than three”);
}
Page 11 of 12
jOptionPane.showMessageDialog(null, “unknown”);
}
13. Rewrite the following code using for loop:
int i = 0;
while(++i <20)
{
if(i = = 8)
break;
System.out.println(++i);
}
Ans: for(int i = 0;++i<20;)
{
if(i = = 8)
break;
System.out.println(++i);
}
Page 12 of 12