Assignment Based on Selection
Control and Loop Controls
1.
What is the difference between exit controlled and
entry controlled loop?
|
2.
Find out the value of variable b.
int b=0 ;
for(int
a=0;a<=30;a++)
{
if( a % 2==0)
{
b=
b+a;
else
{
b=b;
}}
System.out.println(“”+b);
|
3.
Write the sample code to check whether password is
STUDENT or not if not display proper message “Password is wrong” ?
|
4.
What will be the value of
Y if the value of X is given 21
(i)Y=
++ X; (ii) Y= X++;
5.
Rewrite the following
program code using do…while loop
int a, sum =0;
for (a=1; a< =50 ; a= a+2)
{
Sum = sum +a;
}
6.
The following code has
some error(s).Rewrite the correct code underlining all the correction made:
Int I , sum = 0;
while ( I <= 20
{
Sum += i
i += 2;
}
jTextField1.getText ( “ “ +sum);
7.
What will be the contents
of jTextField1 and jTextField2 after executing the following
Code
String str = “java GUI Programming”;
jTextFiled1.setText(str.tolowercase( ));
jTextField2.setText(str. Substring(5,8));
8. Rewrite the code using While Loop?
int j,i=5;
for (j=1;j<=i;j++)
{
System.out.println(
“ “+j);
}
9. What will be the contents of
jTextarea1 after executing the following code:
jTextarea1.setText(“Kendriya\n
Vidyalaya\t Guna”);
10. Rewrite the code using switch
statement:
If(k==1)
Day=”Monday”;
elseif(k==2)
Day=”
Tuesday”;
elseif(k==3)
Day=”
Wednesday”;
else
Day=”-”;
11. Rewrite the following program code using a for loop:
Int i=1,sum=0;
While(i<10)
{
sum+=i;
i+=2;
}
12. The following has some error(s).Rewrite
the correct code underlining all the corrections made:
Int i, 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”)
13. What value for X
below will print Exactly “Line 20” lines
to standard output:
int count=0;
while(++count<X)
jTextField1.setText(“Line
“ + count);
14.
Rewrite
the correct code underlining all the corrections made:
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”);
15.
Rewrite
the following program code using for loop:
int num=6;
int num=6;
int
temp=num;
while(num>=1)
{
temp=temp-1;
if
(temp%2==0)
System.out.println(“Is
even”);
else
System.out.println(“Is
odd”);
num=num-2;
}
16.
Write
the output of the program: 1
int x = 10;
int y = 15;
jTextField1.setText(“”+(x>y)?
3.14: 3);
17.
What
will be the contents of jTextField after executing the following
statement? jTextField.setText
( ‘B’ + ‘a’ );
18.
What
will be the output of the program given below. Which number is printed twice?
(Show the working also
int
sum1=3;
sum1++;
jTextField1.setText(“”+sum);
++sum1;
jTextField2.setText(“”+sum1);
jTextField3.setText(“”+(++sum1));
jTextField4.setText(“”+sum1++);
1jTextField5.setText(“”+sum1);
19.
What
will be the value of M and N after execution of the following code:
int M=150,N;
for(N=10;N<=12;N++)
M+=N;
JOptionPane.showMessageDialog(this,”M
: ” + M +”N: “+ N+””);
20. What
will be values of x and y
after execution of the following code :
int x, y = 0;
for( x= 1; x<=5; ++x)
y=x++;
--y;
int x, y = 0;
for( x= 1; x<=5; ++x)
y=x++;
--y;
21. Write
a function in Java that takes principal, rate and time as parameter and returns
Simple
Interest.
Interest.
22. Rewrite
the following code using a for loop :
int i=1, sum=0;
while (i<10)
{
sum+=i;
i+=2;
}
int i=1, sum=0;
while (i<10)
{
sum+=i;
i+=2;
}
23. Analyze
the program segment and determine how many times the body of loop will be
executed
x=5 ; y=50;
while (x<=y)
{ y=y/x;
jTextField1.setText (“”
+y) ; }
24. Write
a JAVA code that takes a character from JTextField1 and find whether it is a
vowel or not and display it in JtextField2.
25. State the output of the given code:
int tick=10;
int k;
for(k=1; k<=10; k=k+2)
{ if (tick%
k ==0 )
jTextField1.setText( “ “+k + “ is a factor of “ + tick);
else
jTextField1.setText(Integer.toString(k));}
26. Rewrite the code Using Switch case -
char c = ‘g’;
if(c = = ‘g’)
string
x = “Greetings” ;
if(c = = ‘b’)
string
x = “Bye” ;
if(c = = ‘d’)
string
x = “ok”;
else
string x = “Sorry! No Message Available”;
27.
Name the method used to retrieve the
selected item from jComboBox.
28. What’s
wrong with the while statement?
while((ctr<5) &&(ctr>30))
{ int i=i++;}
29. double
number;
string sn;
number=
Double.parseDouble(jTextField1.getText( ));
JTextField2.setText(Integer.toString(sn.length(
)));
30. How
many variables and objects have been declared in the above code?
31. Name
any one native class along with its methods .
32.
What will be the contents of jTextarea1
after executing the following code:
1.
jTextArea1.setText(“RED\n SCHOOL\t
CHHUCHHAKWAS”);
33.
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”);
34.
Convert the following code into while
loop.
int m,n;
m=5; n=9;
for
( int j=0; j<=10; j+=2 )
{
System.out.println( m + “ “ + n );
m = m + 10;
n = n + 5;
}
35.
Write two differences between a listbox
and combobox.
36.
The following code has some error(s).
Rewrite the correct code and underlining all the corrections made:
int
sum; value; inct;
int
I;
for(i
= = 0, i <= 10, i++)
sum += i;
inct ++;
37. What will be the contents of jTextfield1 and
jTextfield2 after executing the following code: 1
String s=”RED CHHUCHHAKWAS”
jTextField1.setText(s.Length()+” “ );
jTextField2.setText(Math.pow(2,3)+“”);
38. What will be the value of x and y after execution
of the following code:
int
x , y = 0;
for(x
= 1; x <= 5 ; x + + )
y
= x + + ;
-
-y;
System.out.println(
x + “ “ + y );
3 9.
Write a method in JAVA that takes a
number as parameter and returns its reverse
40.
What is the output of the following
code, if the value of variable k is 10?
int
result = ++k + k++ + k ;
System.out.println ( result ) ;
System.out.println ( result ) ;
41.
Write a program in JAVA to input a
number in jTextField1 and calculate its factorial.
42.
What are expressions? How many types of
expressions are there? Name them.
43.
Differentiate between Entry-Controlled
Loop and Exit-Controlled Loop.
44.
Write the code for the following :
1
1 2
1 2 3
1 2 3 4
45. 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 switch case.
46. What
will be the output produced by following code fragment?
x=1 ; y=1;
if(n>0) {
x=x+1 ;
y=y-1 ;
}
What will be the value
of x and y, if n=1 & n=0
47. Find
out errors if any:
int i; sum= 0;
i=1 ;
while (i<=10)
{
Sum =sum +i
i+=3
}
System.out.println(sum) ;
48. Predict
the output of the following code fragments:
int
val1 =5; val2=10;
for(int
i=1;i<=3;i++)
system.out.println(“
“ +val1++ + “,” + --val2);
system.out.println(“
“ +val2-- + “,” +++va12);
49. Distinguish
between a unary, a binary and a ternary operator, Give examples of Java
operators for each one of them?
50. What
is wrong with following code:
int n = 2
Switch ( n)
{
Case 1:
a1 = 10;
a2 = 20;
break;
Case 2:
a3 = 30;
break;
a4 = 40;
}
51. What
is the purpose of continue statement in loop?
52. Why
is it necessary to put break statement after every case in a switch statement?
53. How
many times the following loops be executed:
LOOP-1 LOOP-2
int a=5,b=25; int a=5,b =25;
while (a >=b) while (a<=b)
{ {
a = a + 10; a=a+10
} }
54. Write a function in Java that accepts two numbers as Parameters
and returns their product.
55. What
will be the value of n after executing the following code:
int n = 0;
if (6 >7 | | 5>4)
{
n = 5;
}
else
{
n = 7 ;
}
56. What
will be the contents of jTextArea1 after executing the following statement?
jTextArea1.setText(“Hello
\n How are You \t Rohit?”);
57. Rewrite
the following code using while loop 2
int sum = 0; j = 5;
for (i=0;i<=4; i++)
{
sum = sum
+(i+j-1);
}
58. Identify
the error(s) in the following code fragment:
int vowels = 0;
switch (ch)
{
case „a‟ :
case „A‟ :
case „e‟ :
case „i‟ :
case „I‟ :
case „o‟ :
case „O‟ :
case „u‟ :
case „U‟
:vowels++;
}
59. What
will be the contents of jTextField1 and jTextFiled2 after executing the
following code:
jTextField1.setText(Math.pow(3,4)+
“”);
jTextField2.settext(Math.round(23.172)+
“”);
60. Differentiate
between a Text Field, Text Area and password field.
|
61. What
is the difference between if and switch statements?
|
62. What
are the operators supported by Java explain logical operators?
63. Given
the following expression:-
a ) res= 30; b )
res==30;
64. What is the
difference between these two statements?
65. What will be
the value of res if initial value of res is equal to 48
66. Write
a java code for the Item event handler of a checkbox(namely incCB) that increments
a variable total and display it on a label(namely count) the checkbox is
selected.
67. State
the output of the following code:
int a = 10 , b = 5 ;
if
( a > b ) {
if
( b > 5 )
tf1.
setText ( " b is " + b )
;
}
else
tf1. setText (
"a is " + a ) ;
68. What
will be the output of the following code fragment when the value of ch is ‘B’.
switch
(ch) {
case
‘A’ : ta1. append (" Grade A " );
case ‘B’ : ta1. append (" Grade B
" );
case
‘C’ : ta1. append (" Grade C " );
case
‘D’ : ta1. append (" Grade D " );
break ;
default : ta1. append (" Grade F " );
}
69. Rewrite
the following code using do-while loop.
for
( i = 0 ; i < 10 ; i ++ )
ta1. append ( 2 + " x " + i + " = " + ( 2 * i ) )
;
70. Rewrite
the following program code using a switch statement :
if
( code = = 1 )
Month
= "January" ;
else
if ( code = = 4 )
Month = "April" ;
else
if ( code = = 8 )
Month = "August" ;
else
Month =
"No Match" ;
71. The
following code has error(s). Rewrite the correct code underlining all the
corrections made:
int Sum = 0 , Step = 5 ;
int I ;
for ( i = 0 ; i < = 5 , i ++) ;
{
Step
+= 5 ,
Sum += Step ;
}
jTextArea1
. showText ( " " + Sum )
72.
Find error :- 1
if (sex==1)
jLabel1.setText(“Male”);
else ;
jLabel1.setText(“Female”)
73. What
will be the value of x and y after execution of following code : 1
int x , y = 0 ;
for
( x = 1 ; x < = 5 ; ++x )
y = x + + ;
-
- y ;
74. What
will be the value of P and Q after execution of the following code? 1
int
P,Q=100;
for(P=10;
P<=12;P++)
{
Q
+= P;
}
System.out.println(P);
System.out.println(Q);
75. Rewrite
the following program code using a Switch statement:
if(
code == 1 )
Month
= “January”;
else
if ( code == 2 )
Month
= “February”;
else if ( code == 3 )
Month
= “March”
else if ( code == 4 )
Month
= “April”;
else
Month
= “No Match”;
76. What
will be displayed in jTextField1 after executing the following code?
int m =
16;
m
= m + 1;
if ( m < 15 )
jTextField1.setText(Integer.toString(m));
else
jTextField1.setText(Integer.toString(m+15));
77.
What will be the contents of jTextField1 after
executing the following statement :
int n=20;
n=n-6;
if(n<10)
jTextField1.setText(Integer.toString(n));
else
jTextField1.setText(Integer.toString(n-2));
78.
The following code has some error(s). Rewrite the
correct code underlining all the corrections made:
Int n1,n2=10;
n1=n2-5;
if(n1=n2)
jTextField1.setText(“n1 and n2 are
equal”);
else
jTextField1.setText(“n1 and n2 are
not equal”);
79. The
Admission No. of a student is stored in a variable strAdmno. Sugandha wants
to store this admission no. in an
Integer type variable intAdmno. What java statement should she write to do
this?
80. Rewrite
the following program using a if
statement. 2
int
c=jComboBox1.getSelectedIndex();
switch(c)
{ Case
0: FinalAmt= billAmt; break;
Case
1: FinalAmt= 0.9*billAmt; break;
Case2: FinalAmt=0.8* billAmt; break;
default: FinalAmt= billAmt; break;
}
81. How
many times the following while loop get executed?
int p=5;
int q=36;
while(p<=q)
{ P+=6;
}
82. Given
a string object named Salary having value as “55000” stored in it. Obtain the
output of the following.
JOptionPane.showMessageDialog(null,””+Salary.length()+Integer.parseInt(Salary)));
|
No comments:
Post a Comment