0% found this document useful (0 votes)
78 views12 pages

GRADE 11th JAVA CONCEPTS

The document provides information about Java programming using Swing APIs for GUI development. It discusses that Swing is used for GUI programming in Java and provides more powerful and flexible components than AWT. It then lists and describes commonly used Swing controls like jFrame, jLabel, jTextField, jButton, jList, jComboBox, jRadioButton, jCheckBox and others. It also includes some sample code and questions/answers related to Java programming and Swing controls.

Uploaded by

amelia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views12 pages

GRADE 11th JAVA CONCEPTS

The document provides information about Java programming using Swing APIs for GUI development. It discusses that Swing is used for GUI programming in Java and provides more powerful and flexible components than AWT. It then lists and describes commonly used Swing controls like jFrame, jLabel, jTextField, jButton, jList, jComboBox, jRadioButton, jCheckBox and others. It also includes some sample code and questions/answers related to Java programming and Swing controls.

Uploaded by

amelia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Billabong High International School, Noida

Notes for Unit5


(2022–23)

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.

Some Important Questions with Answers


1. Which window is used to design the form?
Ans: Design window
2. Which window contains the Swing Controls components?
Ans: Palette window
3. What is the most suitable component to accept multiline text?
Ans: Text Area
4. What will be the output of the following command?

Page 1 of 12
Learning.concat("Java")
Ans: Error

5. What will be the output of the following command?


"Learning".concat("Java")
Ans: LearningJava
6. Name the different list type controls offered by Java Swing.
Ans:
(i) jListBox
(ii) jComboBox
7. Name any two commonly used method of ListBox.
Ans: getSelectedIndex() and getSelectedValue()
8. Write code to add an element (“New Course”) to a list (SubList) at the beginning of the list.
Ans: SubList.add(0,”New Course”);
10. By default a combo box does not offer editing feature.How would you make a combo box
editable.
Ans: By setting its editable property to false.
11. Write the name of component classes of Swing API for the following components- a) frame (b)
button
Ans: (a) JFrame (b) JButton
12. What is the name of event listener interface for action events?
Ans: ActionPerformed
13. What does getpassword() on a password field return?
Ans: A character array.
14. What is event driven programming?
Ans: This programming style responds to the user events and is driven by the occurrence of user events.
15. What are containers? Give examples.
Ans: Containers are those controls inside them e.g., frame (JFrame), Panel (JPanel), label (JLabel) etc. are
containers.
16. Which method of list is used to determine the value of selected item, if only one itm is selected?
Ans: getSelectedValue()
17. Which type of storage is provided by variables?
Ans: Temporary
18. What will be the output of the following code segment:
String firstName = "Manas ";
String lastName = "Pranav";
String fullName = firstName + lastName;
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);
Ans: Full Name: ManasPranav
19. Which expression is used to print the value of a variable "x" of type int?
Ans: jTextField1.setText("x = " + x);
20. The statement i++; is equivalent to:
Ans: i= i+1
21. Name the primitives datatypes in java.
Ans: Numeric type , Fractional type, Character type and Boolean type.

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

OUTPUT FINDING QUESTIONS


1 Write the output following code when executed:
(i) System.out.printl(“Hello”.charAt(3));
(ii) System.out.printl(“Good morning”.substring(4));
Ans: (i) l (ii) morning
2. Write the output of the following code:
int x , y = 0;
for(x=1;x<=5;++x) y = x++;
--y ;
Ans: 7 4
3. Find the output of the code:
Ans: 24
int f=1,i=2; do
{ f*=i;
}while(++i<5); System.out.println(f);
4. What will be the value of j and k after execution of the following code:
int j=10,k=12; if(k>=j)
{
k=j; J=k;
}
Ans: 10 10
5. How many times, the following loop gets executed?
i=0;
while (i> 20)
{
//Statements
}
Ans: 0 times
6. How many times, the following loop gets executed?
i=0;
do
{

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

ERRORS FINDING QUESTIONS


1. Rewrite the following code after removing the errors(if any). Underline the corrections.
int i=2,j=5;
while j>i
jTextField1.getText(j is greater);
j--; ++i;
}JOptionPane.showMessageDialog(Hello);
Ans:
int i=2,j=5;

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”);
}

12. Rewrite the following code fragment using switch :


if(ch = = ‘E’) east++;
if(ch = = ‘W’)
west++; if(ch = = ‘N’)
north++; if(ch = = ‘S’)
south++;
else
jOptionPane.showMessageDialog(null, “unknown”);
Ans. switch(ch)
{
case ‘E’:
east++;
break;
case ‘W’:
west++;
break;
case ‘N’:
north++;
break;
case ‘S’:
south++;
break;
default :

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

You might also like