0% found this document useful (0 votes)
2 views

CBSE Class XI Informatics Practices Introduction to Programming Questions

The document contains a series of important questions and answers related to Java programming and Swing components. It covers topics such as event-driven programming, different types of controls, methods, and programming concepts like loops and conditionals. Additionally, it includes short answer questions explaining key programming terms and concepts.

Uploaded by

sreetej kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CBSE Class XI Informatics Practices Introduction to Programming Questions

The document contains a series of important questions and answers related to Java programming and Swing components. It covers topics such as event-driven programming, different types of controls, methods, and programming concepts like loops and conditionals. Additionally, it includes short answer questions explaining key programming terms and concepts.

Uploaded by

sreetej kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.

COM

Some Important Questions with Answers

1. Which window is used to designed 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?


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.

m
Ans: (i) jListBox

o
(ii) jComboBox

7. Name any two commonly used method of ListBox.

y . c
a
Ans: getSelectedIndex() and getSelectedValue()

t o d
8. Write code to add an element (“New Course”) to a list (SubList) at the beginning of the list.

ies
Ans: SubList.add(0,”New Course”);

d
9. Describe the three common controls. Also give some of their properties.

tu
Ans:

.s
(i) jButton text,icon
(ii) jLabel text,border

w
(iii) jTextField text,font

w w
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 Name the 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.

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

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 = "Johua ";
String lastName = "Yacomo";
String fullName = firstName + lastName;
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);
Ans: Full Name:
JohuaYacomo

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.

m
Ans: Numeric type , Fractional type, Character type and Boolean type.

. co
22. Which events gets fired when a user click a JButton and JRadioButton.

y
Ans: ActionPerformed

23. Which of the following is a selection construct ?

d a
o
a. do while Loop b. for Loop c. while Loop d. None of these

t
Ans: none of these

ies
24. What will be used if there are two or more possible options.

d
Ans: is else if and switch

25. Name the loop that never ends.

s tu
Ans: infinite loop

w .
w
26. Which braces is used to enclose statements in a block statement.

w
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

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Short answers type questions:

1. Explain the following terms:


a) IDE
b) Form
Ans:
a) IDE : IDE is an acronym for Integrated Development Environment which is a work
environment that integrates all tools necessary for Application Development
and makes them available as part of one environment.
b) Forms: Forms are used to accept data (input) and submit data to an external agent for
processing.
2. Explain the usage of the following methods :
a) setText()
b) toString()
c) concat()
Ans:
a) setText() : It is used to change the display text of a component (label, text field or button) during run time.
b) toString() : It is used to convert an Integer value to String type.
c) concat() : The concat() method or the string concatenation symbol(+) may be used to
add two strings together.

m
3. Differentiate between:

o
Ans:

. c
a) Text field and Text area components :

y
The Text Field allows the user to enter a single line of text only. But Text Area component allows to accept

a
multiline input from the user or display multiple lines of information.

b) Text field and Password field components:

t o d
ies
The Text Field displays the obtained text in unencrypted form whreas password field displays the obtained
text in encrypted form. This component allows confidential input like passwords which are single line.

d
c) parseInt() and parseDouble() methods:

u
parseInt() is used to convert a string value to Integer type whereas parseDouble() is used to convert a

t
string value to type Double.

.s
w
4. What is a Variable?
Ans: Variables are named temporary storage locations.

w w
5. Why are data types important?
Ans: Data Types define the way the values are stored, the range of the values and
the operations that can be performed on that type.

6. How are keywords different from variable names?


Ans: Keywords have special meaning in java, should not be used as the variable names. Variables are
named temporary storage locations.

7. What is an identifier?
Ans:Identifiers are fundamental building block of a program and are used as the general terminology for the
names given to different parts of the program viz. variables, objects, classes,functions, arrays etc.

8. What is casting? When do we need it?


Ans:Casting is a conversion, which uses the cast operator to specify the type name in parenthesis and is
placed in front of the value to be converted.
For example: Result = (float) total / count ;
They are helpful in situations where we temporarily need to treat a value as another type.

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


Ans:In a loop, the break statement terminates the loop when it gets executed.

10. Is Java case sensitive? What is meant by case sensitive?


Ans: Yes java is case sensitive. Case sensitive means upper case letters and lower case letters are treated
differently.

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

11. Is a string containing a single character same as a char?


Ans: No

12. What is the main difference between a combo box and a list box?
Ans: The List Box does not have a text field the user can use to edit the selected item, wheras a Combo
Box is cross between a text field and a list.

13. Explain the use of for statement along with its syntax.
Ans: The for loop repeat a set of statements till a test condition is satisfied.
The syntax of the for loop is:
Syntax
for( initialization; test exp; increment/decrement exp)
{
statements;
}

14. What is the difference between selection and repetition?


Ans: Selection statements test conditions and selectively execute code depending on the outcome of the
test condition , whereas repetition statements repeat a set of statements till a test condition is satisfied.

15. What is the purpose of if statement? Name the different forms of if statement.
Ans: The if statement allows selection (decision making) depending upon the outcome of a condition.

m
a) simple if

o
b) if else

c
c) if - else if – else

y .
a
16. What is the purpose of default clause in a switch statement?

d
Ans: The default statement gets executed when no match is found in switch.

t o
17. What is the main difference between a while loop and a do while loop?

ies
Ans: In while loop test expression is evaluated at the beginning of the loop whereas in do while loop the test
expression is evaluated at the bottom of the loop. This means that do-while loop is executed at least once.

tu d
.s
18. How is the if…else if combination more general than a switch statement?

w
Ans:The switch statement must be by a single integer control variable, and each case section must

w
correspond to a single constant value for the variable. The if…else if combination allows any kind of

w
condition after each if.

19. Excessive comments add time to the execution of your program. (True/False).
Ans: False because comments are non executable.

20. Differentiate between compile time and run time errors.


Ans: Compile time errors refer to the errors that violate the grammatical rules and regulations of
programming language.

21. Which error is harder to locate and why?


Ans: Logical errors because in presence of logical error , the program executes without any problems but
the output produced is not correct. Therefore every statement of the program need to be scanned.

22. Explain the following terms:


a) Exception handling : Run time errors are also called exceptions, and handling such errors in the
application is called exception handling.
b) Syntax : Formal set of rules defined for writing any statement in a language is known as syntax.
c) Portability : The application should be portable. It should be able to run on different platforms.
d) Prettyprinting : Prettyprinting is the formatting of a program to make it more readable. These
formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar
modifications intended to make the content easier to view, read and understand.
e) Syntax error: Syntax errors occur when syntax rules of any programming language are violated. These
errors occur during compilation of the application

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

OutPut Finding Questions:

1 Write the output :

(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 :

intx , y = 0;
for(x=1;x<=5;++x)
y = x++;
--y ;
Ans: 7 4

3. Find the output of the code:

o m
c
int f=1,i=2;

.
do
{ f*=i;
}while(++i<5);
ay
System.out.println(f);

t o d
ies
Ans: 24

4. What would the following code do :

tu d
Connection , statement created
.s
w
String str=”select * from emp”;

w
Resultsetrs= stmt.executeQuery(str);

w
rs.last();
int r= rs.getRow();
JOptionPane.showMessageDialog(null,””+r);
Ans : if emp table has 5 records it will display 5

5. What will be the output of the following code segment:

String firstName = "Johua ";


String lastName = "Yacomo";
String fullName = firstName + lastName;
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);

Ans: Full Name :


JohuaYacomo
6. 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

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

7. How many times, the following loop gets executed?

i=0;
while (i> 20)
{
//Statements
}
Ans: 0 times

8. How many times, the following loop gets executed?

i=0;
do
{
//Statements
}while (i> 20);

Ans: 1 time

9. What will be the contents of jTextield1 and jTextField2 after executing the following statement:

StringBuffer s= new StringBuffer(“Common Wealth”);


Int c=s.capacity();

o m
c
s.insert(0,’E’);

.
s.reverse();
jTextField1.setText(“”+c);

ay
d
jTextField2.setText(s.toString());

o
Ans:

t
ies
29

htlaeWnommoCE

tu d
.s
10. What will be the contents of jTextield after executing the following statement:

int num=4;
ww
w
num=num+1;
if(num>5)
jTextField1.setText(Integer.toString(num));
else
jTextField1.setText(Integer.toString(num*4));

Ans : 7

11. Find the output of the following code:

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

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

12. 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

13. How many times will the following loop get executed?

x = 5;
y = 36;
while ( x <= y)
{
x+=6;
}

Ans: 6

o m
y . c
14. What will be the content of the jTextArea1 after executing the following code?

a
Int Num = 1;

d
do

o
{

t
ies
jTextArea1.setText(Integer.toString(++Num) + "\n");
Num = Num + 1;
}while(Num<=10)

d
Ans: 10

s tu
.
15. What will be the contents of jTextfield1 and jTextfield2 after executing the following code:

w
w
String s=”KENDRIYA VIDYALAYA GUNA”

w
jtextfield1.setText(s.length()+” “);
jtextfield2.setText(Math.round(2.34)+“”);

Ans : 23 2

16. 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

17. 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

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

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 “ ) ;

m
Ans: Infinite loop

. co
20. Give the value of x after executing following Java code. Also find how many times the

y
following loop will execute? :

a
int a=10;

d
int b=12;

t o
int x=5;

ies
int y=6;
while (a<=b)
{ if (a%2= =0)

else
x=x + y;

tu d
.s
x=x-y;
a=a+1;
}
Ans:11
ww
w
21. What will be the output produced by following code fragment? (1)

flaot x=9;
float y=5;
int z=(int)(x/y);
switch(z)
{
case1:x=x+2;
case2: x=x+3;
default:x =x+1;
}
System.out.println(“value of x:”+x);

Ans: 15

22. Predict the output of the following code fragments:

inti,j,n;
n=0;i=1;
do
{ n++; i++;
}
DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM
DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

while(i<=5);
Ans: 5

23. What will be the output of the following program code when the user will press JButton:

Public class svm


{ int a;
svm(int p)
{ a=p; }
void assign(int no)
{ a=no; }
intdisp()
{ return a; }
}
private void jButton1ActionPerformed(java.awt.event.ActionEventevt)
{
svm os=new svm(15);
System.out.println(“ “ + os.disp());
os.assign(35);
System.out.println(“ “ + os.disp());
}

m
Ans: 15 35

. co
y
24. What will be the contents of jTextField1 and jTextField2 after executing the following code:

String s = “Sun Micro Systems”;


d a
jTextField1.setText(s.length()+””);

t o
ies
jTextField2.setText(s.toLowerCase());
Ans:

d
jTextField1 : 17

tu
jTextField2 : abc micro systems

.s
w
25. What values will be assigned to the variable ua ,ub, uc and fail after execution of the following program

w
segment:

while(i<=5) {
switch ( i++ )
w
int i=0,ua=0,ub=0,uc=0,fail=0;

{ 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

26. Predict an output of the following code fragments:

int i = 1, j = 0, n = 0;
while(i<4) {
for(j=1;j<= i ; j++)
{ n+= 1;
i = i+1;
}
System.out.println(n);
}
Ans : 6

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

27. Give the output of the following code:

int m=100;
while(m>0)
{
if (m<10)
break;
m=m-10;
}
System.out.println(“m is “+m);
Ans: 0

o m
y . c
d a
t o
d ies
s tu
w .
w w

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Errors finding questions:

1. The following code has some errors. Rewrite the corrected code .
int i=2,j=5;
while j>i
{ jTextField1.getText(“j is greater”);
j--; ++i;
}JOptionPane.showMessageDialog(“Hello”);
Ans:
int i=2,j=5;
while( j>i)
{ jTextField1.getText(“j is greater”);
j--; ++i;
}JOptionPane.showMessageDialog(“Hello”);
2. Identify the errors :

m
switch(ch)

o
{ case ‘a’ :
case ‘A’ :

y . c
a
case ‘e’ :
case ‘E’ :

t o d
ies
case ‘i’ :
case ‘i’ :
case ‘u’ :

tu d
s
case ‘U’ : ++vowels;

w
break;
.
w
default : ++others;

w
Ans: two case constants doesn’t have the same value
3. inti,j=5;
i==j+5;
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”);
Ans:
int i,j=5;
i=j+5;
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”);

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

4. Rewrite the code after making correction . 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++;

5. The following code has some error(s). Rewrite the correct code underlining all the corrections made.

int y=3;
switch(y);
{ case 1: System.out.print(“Yes its One”);
case>2: System.out.println(“Yes its more than Two”);

o m
c
break;

.
case else: System.out.print(“Invalid Number):

ay
d
ans:

o
int y=3;

t
switch(y)

ies
{ case 1: System.out.print(“Yes its One”);
break;

d
case 2: System.out.println(“Yes its more than Two”);

tu
break;

s
default:System.out.print(“Invalid Number):

.
}

ww
w
6. The following has some error(s).Rewrite the correct code underlining all the corrections made:

Inti,j=5;
i==j+5;
if(i=j)
{
jtextfield1.setText(“I and j are unequal”);
jtextfield1.setText(“I and j are not equal”);breaks;
}
else
jtextfield1.setText(“I and j are equal”)
Ans:

inti,j=5;
i=j+5;
if(i==j)
{
jTextField1.setText(“I and j are unequal”);
}
else
jTextField1.setText(“I and j are equal”)

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

7. Rewrite the following Java code after underling the corrections made.

int x = = 0;

int n= Integer parseInt(Jlabel1.getText);

Ans:

int n= Integer parseInt(JLabel1.getText());

int x = 0;

8. Find out errors if any:

M=1;
N=0;
For(;m+n<19;++n)
System.out.println(“hello”);

m
M=m+10;

Ans:

. co
y
m=1;

a
n=0;

d
for(;m+n<19;++n)

o
System.out.println(“hello”);

t
m=m+10;

ies
9. The following code has some error(s). Rewrite the correct code underlining all the corrections made.

d
int y=6,p;

tu
do

s
{ y=3.14*y;

if p=2
p=y%10;

w .
w
System.out.print(“Two”);

w
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);

Rewrite questions:

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

inti,sum=0;

while(i<10)

{ sum +=i;

i+=2;

}
DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM
DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Ans: int i,sum=0;

for(i=0;i<10;i+=2)

{ sum +=i;

2. Rewrite the following code using while loop :


inti,j;
for(i=1;i<=4;i++)
{ for(j=1;j<=i;++j)
{ System.out.print(j);
}
System.out.println();
}
Ans:
int i=1,j;

o m
while(i<=4)

y . c
a
{ j=1;

d
while(j<=i)
{ System.out.print(j);
t o
ies
++j;

d
} i++;
System.out.println();

s tu
}

w .
3. Write a equivalent while loop for the following code:
intsz=25;

w w
for(int i=0,sum=0;i<sz;i++)
sum+=i;
System.out.println(sum);

Ans: int sz=25;

int i=0,sum=0;

while(i<sz)
{
sum+=i;
i++;
}
System.out.println(sum);
4. Rewrite the following if-else segment using switch-case statement
char ch='A';
if(ch=='A')
System.out.println("Account");

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

if((ch=='C') || (ch=='G'))
System.out.println("Admin");
if(ch=='F')
System.out.println("Advisor");

Ans: char ch='A';

switch(ch)
{
case ‘A':
System.out.println("Account");
break;
case 'C':
case 'G’:
System.out.println("Admin");
break;
case 'F':
System.out.println("Advisor");

5. Rewrite the following code using while loop:

o m
. c
inti,j;

y
for(i=1,j=2;i<=6;i++,j+=2)

a
System.out.println(i++);

d
System.out.println(“Finished!!!”);

t o
ies
Ans: inti=1,j=2;
while(i<=6)

d
{System.out.println(i++);

u
i++;

t
j+=2;}

.s
System.out.println(“Finished!!!”);

ww
6. Rewrite the following code using for loop.

w
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++);
}

7. Rewrite the code using switch statement:

If(k==1)
Day=”Monday”;
elseif(k==2)
Day=”Tuesday”;
elseif(k==3)
Day=”Wednesday”;
else
DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM
DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Day=”-”

Ans:
switch(k)
{
case 1: Day=”Monday”;
break;
case 2: Day=”Tuesday”;
break;
case 3: Day=”Wednesday”;
break;
default: Day=””;
}

8. Write the equivalent switch case for the following code :

If (num1 = =1 )
jTextField1.setText(“Number is one”);
else If (num1 = =2 )

m
jTextField1.setText(“Number is two”);

o
else If (num1 = =3 )

c
jTextField1.setText(“Number is three”);
else
jTextField1.setText(“Number is more than three”);
y .
Ans:

d a
t o
ies
Switch(num1)
{
Case 1 :
jTextField1.setText(“Number is one”);
break;

tu d
.s
case 2 :
jTextField1.setText(“Number is two”);
break;
case 3 :
ww
w
jTextField1.setText(“Number is three”);
break;
default:
jTextField1.setText(“Number is more than three”);
}

9. Given the following code fragment :

If(a==0)
System.out.println(“zero”);
If(a==1)
System.out.println(“one”);
If(a==2)
System.out.println(“two”);
If(a==3)
System.out.println(“three”);

Write an alternative code (Using if) that saves on number of comparsons

Ans:

if(a==0)
System.out.println(“zero”);

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

else if(a==1)
System.out.println(“one”);
else if(a==2)
System.out.println(“two”);

else if(a==3)
System.out.println(“three”);

10. 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:

m
Switch(ch)

o
{

Case ‘E’:

y . c
a
east++;

d
break;

t o
case ‘W’:

ies
west++;
break;
case ‘N’:
north++;
break;
tu d
.s
case ‘S’:

w
south++;
break;
default :

w w
jOptionPane.showMessageDialog(null, “unknown”);
}

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

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Design Questions:

1. Design an application for Theatre Booking system. And answers the following questions :

m
(a) When the user select different seat type, then its price should be displayed in the Label.

o
c
(b) If the user enters an invalid no of seats i.e. less than I, then an error message should be displayed in

.
the dialog box.

y
( c) When the user click at the Book Seats button , then total amount (calculated as no. of seats x price per

a
seat) should be displayed along with payment method, next to the push button.

d
(c) Price per seat depend upon the seat type :
Stall 625/-

t o
ies
Circle 750/-
Upper Circle 850/-

d
Box 1000/-

u
Ans:

.s t
(a) if(jRadioButton1.isSelected()==true)

w
jLabel2.setText(“625”);

w
if(jRadioButton2.isSelected()==true)

w
jLabel2.setText(“750”);
if(jRadioButton3.isSelected()==true)
jLabel2.setText(“850”);
if(jRadioButton4.isSelected()==true)
jLabel2.setText(“1000”);
(b) int s=Integer.parseInt(jTextField1.getText());
if(s<1)
JOptionPAne.showMessageDialog(null,”Error”);
(c) int s=Integer.parseInt(jTextField1.getText());
int p=Integer.parseInt(jLabel2.getText());
int tp=s*p;
if(jRadioButton5.isSelected()==true)
jLabel5.setText(“Cash Payment of “ +tp);
if(jRadioButton6.isSelected()==true)
jLabel5.setText(“Visa Payment of “ +tp);
if(jRadioButton7.isSelected()==true)
jLabel5.setText(“American Exress Payment of “ +tp);
if(jRadioButton8.isSelected()==true)
jLabel5.setText(“Master Card Payment of “ +tp);

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

2. Write a java program that lets you create an address book in a table. The details to be
added in Address Book are : SNo,Name, Email Id, Phone.

Ans:

DefaultTableModel tm= (DefaultTableModel) jTabel1.getModel();

m
int sno= Integer.parseInt(jTextField1.getText());

String name = jTextField2.getText();

. co
String email = jTextField3.getText();

ay
long ph= Integer.parseInt(jTextField4.getText());

t o d
ies
Object nr[]={sno,name,email,ph};

tm.addRow(nr);

tu d
.s
ww
w

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

3. Design the following application and answer the questions that follow :

(a) Write the code for the Clear button to clear all the textfields and checkbox. Set the default choice in the
radio button as Fixed Deposit.
(b) Write the code for the calculate button to calculate compound interest and amount and display the
values in the txtInterest and txtAmount depending on principal, rate and time.
Rate is calculated based on the time according to the following table:

m
Account Time Rate

o
Fixed Deposit <= 1 10%

. c
>1 and <=5 12%

y
>5 15%

a
Recurring Deposit <= 2 11%

d
>2 and <=7 12%

t o
>7 15%

ies
An additional rate of 2% is given to the senior citizens i.e. if the chkSR checkbox is checked.

Ans:

(a) jTextField1.setText(“”);

tu d
.s
jTextField2.setText(“”);

w
jTextField3.setText(“”);

w
w
jRadioButton1.setSelected(true);

jCheckBox1.setSelected(false);

(b) int p= Integer.parseInt(jTextField1.getText());


int t= Integer.parseInt(jTextField2.getText());
if(jRadioButton1.isSelected()==true )
{
if(t<=2)
r=11;
else if(t>2 && t<=7)
r=12;
else
r=15;
}

else
{
if(t<=1)
r=10;
else if(t>1 && t<=5)
r=12;

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

else
r=15;
}

float ci= p*Math.pow((1+(r/100)),t);


float amt= p+ci;
txtInterest.setText(“”+ci);
txtAmount.setText(“”+amt);

4. Answers the following questions:

o m
y . c
d a
t o
ies
The grading criteria for the two streams is given below :

d
Stream Percentage Grade
Medical >=80

s tu A

.
60-80 B

ww <60 C

w
Non-Medical >=75 A
50-75 B
<50 C
(a) Write code for Calculate Percentage button to calculate the Percentage after finding the total marks of I
term and II term . Also ensure that NCC cadet gets an increment of 3% in their percentages.
(b) Write code for Calculate grade button to calculate the grade depending upon on the
stream selected according to the given criteria.

Ans:

(a) float f= Integer.parseInt(jTextField1.getText());


float s= Integer.parseInt(jTextField2.getText());
float tot = f+s;
float p= tot/2;
if(jCheckBox1.isSelected()==true)
p=p+3;
jLabelp.setText(“”+p);

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

( b) String g;

if(jRadioButton1.isSelected()==true)
{
if(p>=80)
g=”A”;
else if(p>=60 &p<80)
g=”B”;
else
g=”C”;
}
else
{
if(p>=75)
g=”A”;
else if(p>=50 &p<75)
g=”B”;
else
g=”C”;

o m
c
jLabelp.setText(“”+p);
jLabelg.setText(“”+g);

y .
d a
t o
5. Mr. Madhav works in a construction company. To calculate total wages he has developed the following

ies
GUI in NetBeans.

tu d
.s
ww
w
Male and female labours are respectively paid Rs. 150/- per day and Rs. 170/- per day. Skilled labourers
are paid extra at the rate of Rs. 100/- day. Male and female labourers from rural areas are paid 10% less
per day.

(a) When Calculate Wage button is clicked, the total wages is calculated as per the given criteria and
displayed in total wage text box.

(b) When Clear button is clicked, all the text boxes should be cleared and radio button, check box should
be deselected.
(c) Close the application when Quit button is pressed.

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Ans:

(a) int d =Integer.parseInt(jTextField2.setText());


int w;
if(jRadioButton1.isSelected()==true)
w=150;
else
w=170;
if(jCheckBox1.isSelected()==true)
w=w+100;
if(jRadioButton3.isSelected()==true)
w=w-(w*10)/100;
int cw=d*w;
jLabel6.setText(“”+cw);

(b) jTextField1.setText(“”);

jTextField2.setText(“”);

jRadioButton1.setSelected(false);

jRadioButton2.setSelected(false);

o m
jRadioButton3.setSelected(false);

y . c
jRadioButton4.setSelected(false);

d a
jCheckBox.setSelected(flase);
t o
ies
(c) System.exit(0);

tu d
.s
6. Mr. JigneshDesai an owner of Alpha Chemicals PVT ltd has asked his programmer Sweta to

w
develop the following GUI application in Netbeans:

w w

Service Charges Rates are as follows :

Class of City Rate of Service Charges


i 10% of sales price
ii 15% of sales price
iii 20% of sales price

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Write java code for the following:

(a)To calculate service charges depending on the selection of radio button. This code will execute after
click on the calculate service charges?

(b)To calculate net price when Calculate Net price button will be clicked.

(c)When exit button will be clicked application should be automatically closed.

Ans:

(a) float q=Float.parseFloat(jTextField2.getText());


float p=Float.parseFloat(jTextField3.getText());
float sp=q*p;
jLabelsp.setText(“”+sp);
float sc;
if(jRadioButton1.isSelected()==true)
sc=(10*sp)/100;

m
else if(jRadioButton2.isSelected()==true)
sc=(15*sp)/100;

. co
y
else

a
sc=(20*sp)/100;
jLabelsc.setText(“”+sc);

t o d
ies
float sp=Float.parseFloat(jLabelsp.getText());

u d
float sc=Float.parseFloat(jLabelsc.getText());

t
s
float np=sp+sc;

w
jLabelnp.setText(“”+np);
.
w
(c) System.exit(0);

w
7. Assume the following interface built using Netbeans used for bill calculation of a ice-cream
parlor. The parlor offers three verities of ice-cream - vanilla, strawberry, chocolate. Vanilla ice- cream
costs Rs. 30, Strawberry Rs. 35 and Chocolate Rs. 50. A customer can chose one or
more ice-creams, with quantities more than one for each of the variety chosen. To calculate the
bill parlor manager selects the appropriate check boxes according to the verities of ice-cream
chosen by the customer and enter their respective quantities.

Write Java code for the following:


a. On the click event of the button 'Bill', the application finds and displays the total bill of the
customer. It first displays the rate of various ice-creams in the respective text fields. If a
user doesn't select a check box, the respective ice-cream rate must become zero. The bill
is calculated by multiplying the various quantities with their respective rate and later
adding them all.
b. On the Click event of the clear button all the text fields and the check boxes get cleared.
c. On the click event of the close button the application gets closed.

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

Ans:
(a) private void jBtnCalculateMouseClicked(java.awt.event.MouseEvent evt)
{
if(jchkStrawberry.isSelected()==true)
jTxtPriceStrawberry.setText("35");

o m
c
else
{

y .
a
jTxtPriceStrawberry.setText("0");

d
jTxtQtyStrawberry.setText("0");
}

t o
ies
if(jChkChocolate.isSelected()==true)
jTxtPriceChocolate.setText("50");
else
{
tu d
.s
jTxtPriceChocolate.setText("0");

w
jTxtQtyChocolate.setText("0");

w
}

w
if(jChkVinella.isSelected()==true)
jtxtPriceVinella.setText("30");

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

else
{
jtxtPriceVinella.setText("0");
jTxtQtyVinella.setText("0");
}
int r1,r2,r3,q1,q2,q3,a1,a2,a3,gt;
r1=Integer.parseInt(jTxtPriceStrawberry.getText());
r2=Integer.parseInt(jTxtPriceChocolate.getText());
r3=Integer.parseInt(jtxtPriceVinella.getText());
q1=Integer.parseInt(jTxtQtyStrawberry.getText());
q2=Integer.parseInt(jTxtQtyChocolate.getText());
q3=Integer.parseInt(jTxtQtyVinella.getText());
a1=r1*q1;
jTxtAmtStrawberry.setText(""+a1);
a2=r2*q2;
jTxtAmtChocolate.setText(""+a2);
a3=r3*q3;
jTxtAmtVinella.setText(""+a3);
gt=a1+a2+a3;

m
jTxtTotalAmt.setText(""+gt);
}

. co
(b) private void jBtnClearActionPerformed(java.awt.event.ActionEvent evt)
{

ay
d
jTxtPriceStrawberry.setText("");

t o
jTxtPriceChocolate.setText("");

ies
jtxtPriceVinella.setText("");
jTxtQtyStrawberry.setText("");

d
jTxtQtyChocolate.setText("");

tu
jTxtQtyVinella.setText("");

s
.
jTxtAmtStrawberry.setText("");

w
jTxtAmtChocolate.setText("");

w
jTxtAmtVinella.setText("");

w
jchkStrawberry.setSelected(false);
jChkChocolate.setSelected(false);
jChkVinella.setSelected(false);
}

(c ) private void jBtncloseActionPerformed(java.awt.event.ActionEvent evt)


{
System.exit(0);
}

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

8. ABC School uses the following interface built in java to check the eligibility of a student for a
particular stream from science, commerce and humanities. The user first enters the total
percentage and selects the desired stream by selecting the appropriate option button. An
additional 5% is marks is given to students of NCC.

Write Java Code for the following


a. On Action event of the button 'Calc Percentage' Net percentage of the student is calculated and
displayed in the appropriate text filed. Net percentage is same as that of the actual percentage if the
student doesn't opts for NCC otherwise 5% is added to actual percentage.
b. On Action event of the button 'Result', the application checks the eligibility of the students. And
display result in the appropriate text field. Minimum percentage for science is 70, 60 for commerce and
40 for humanities.
c. On the Click event of the clear button all the text fields and the check boxes get cleared. d. On the
click event of the close button the application gets closed.

o m
y . c
d a
t o
d ies
u
Ans: private void jBtnClearActionPerformed(java.awt.event.ActionEvent evt)
{

.s t
w
jTextField1.setText(" ") OR jTextField1.setText(null)

w
jTextField1.setText(" ") OR jTextField1.setText(null)

w
jTextField1.setText(" ") OR jTextField1.setText(null)
jTextField1.setText(" ") OR jTextField1.setText(null)
jCheckbox1.setSelected(false);

}
private void jBtnCalcPerActionPerformed(java.awt.event.ActionEvent evt)
{
int p;
p=Integer.parseInt(jTextField2.getText());
if (jCheckBox1.isSelected())
p=p+5;
jTextField3.setText(Integer.toString(p));
}
private void jBtnResultActionPerformed(java.awt.event.ActionEvent evt)
{
int p;
p=Integer.parseInt(jTextField3.getText());
if( jRadioButton1.isSelected())
{
if ( p>=70)
jTextField4.setText("Eligible for all subject");

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM


DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

else
jTextfield4.setText("Not Eligible for science");
}

else if( jRadioButton2.isSelected())


{
if ( p>=60 )
jTextField4.setText("Eligible for Commerce and Humanities");
else
jTextfield4.setText("Not Eligible for Science and Commerce");
}
else
{
if ( p>=40 )
jTextField4.setText("Eligible for Humanities");
else
jTextfield4.setText("Not Eligible for any subject ");
}

}
private void jBtnCloseActionPerformed(java.awt.event.ActionEvent evt)

m
{

}
System.exit(0);

. co
ay
t o d
d ies
s tu
w .
w w

DOWNLOADED FROM WWW.STUDIESTODAY.COM DOWNLOADED FROM WWW.STUDIESTODAY.COM

You might also like