CLASS-XII
ASSIGNMENT-1
OF JAVA PROGRAMMING
A. Output Finding Questions:
Q.1.
Write the output of the following code:
int
x,y=0;
for(x=1;x<=5;++x)
y=x++;
--y
Q.2.
Write the output of the following code
Int
f=1,i=2;
do{
f*=I;
}while(++i<5);
System.out.println(f);
Q.3.
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;
}
Q.4.
How many times ,the following loop gets executed?
i=0;
while(i>20)
{
//statements
}
Q,5,
How many times ,the following loop gets executed?
i=0;
do{
//statements
}while(i>20);
Q.6.
What will be the contents of TextField after exec uting the following
statement:
int
num=4;
num=num+1;
if(num>5)
jTextField1.setText(Integer.toString(num));
else
jTextField1.setText(Integer.toString(num*4));
Q.7.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
might work”);
else
jTextField1.setText(“code
will not work ”);
Q.8.
How many times will the following loop get executed?
x=5; y=36;
while(x<=y)
{
x+=6;
}
Q.9.
What will be the content of the jTextArea1 after executing the following code?
int num=1;
do
{
jTextArea1.setText(Integer.to
String(++num)+”\n”);
num=num+1;
}while(num<=10);
Q.10.
Give the output for the following code fragment:
v=20;
do{
jOptionPane.showMessageDialog(numm,v+””);
}while(v<50);
jOptionPane.showMessageDialog(numm,v+””);
Q.11.
Give the value after executing following java code.Also find how many times the
following
loop
will execute?
int
a=10;
int
b=12;
int
x=5;
int
y=6;
while(a<=b)
{
if(a%2==)
x=x+y;
else
x=x-y;
a=a+1;
}
Q.12.What
will be the output produced by following code fragment?
float
x=9;
float
y=5;
int
z=(int)(x/y);
switch(z)
{
case 1:x=x+2;
case
2: x=x+3;
default:
x=x+1;
}
System,.out.println(“value
of x:”+x);
Q.13.
Predict the output of the following code fragment:
int
i,n;
n=0;i=1;
do
{
n++;i++;
}while(i<=5);
Q.13.
What will be the values of x and y after executing the following expressions.
int x=20,y=35;
x= y++ + x++;
y= ++y + ++x;
Q.14.
What will be the values of x and y after executing the following expressions.
int y=-1;
System.out.println(“x:”+(-y--));
System.out.println(“y:”+y);
Q.15.
What will be the values of x and y after executing the following expressions.
int x = 45;
x = x + x++;
System.out.println(x);
int y = 45;
y = y++ + y;
System.out.println(y);
Q.16.
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;
}
Q.17.
Predict an output of the following code fragment:
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);
}
Q..18.
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);
B. Error Finding Questions:
Q.1.
The following cade 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”);
Q.2.
Identify the errors:
switch(ch)
{
case
‘a’:
case
‘A’:
case
‘e’:
case
‘E’:
case
‘i’:
case
‘E’:
case
‘u’:
case
‘U’:++vowels;
break;
default:++others;
Q.3.
int j=5;
i==j+5;
if(i=j)
jTextField1.setText(“I
and j are equal”);
else
jTextField1.settext(“ I and j are unequal”);
Q.4.
Rewrite the code after making correction. Underline the correction.
Int
sum;value;inct;
Int i
For(i==0;i<=10;i++)
Sum=sum+I;
Inct++;
Q.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.println(“yes its one”);
case
>2: System.out.println(“yes it is more than two”);
break;
case
else: System.out.println(“invalid number”);
Q.6.
Rewrite the following java code after underlining the correction made:
int
x==0;
int
n=Integer.parseInt(jLabel1.getText());
Q.7.
find out errors if any:
M=1;n=0;
For(;m+n<19;++n)
System.out.println(“hello”);
M=m+10;
Q.8.
The following code has some error(s).Rewrite the correct code underlining all
the
corrections
made.
int
y=6,p;
Do
{
y=3.14*y;
p=y%10;
If
p=2
System.out.print(“two”);
While(y>1)
C. Rewrite the following program code using a for
loop:
Q1. int I,sum=0;
while(I<10)
{
sum+=
I ;
I+=2;
}
Q.2. 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);
}
System.out.println();
}
Q.3. Write a equivalent while loop for the following
code:
int
sz=25;
for(int
i=0,sum=0;i<sz;i++)
sum+=I;
System.out.println(sum);
Q.4.Rewrite the following if-else segment using switch
–case statement
char
ch=’A’;
if(ch==’A’)
System,out.println(“Account”);
if((ch==’C’)||(ch==’G’))
System.out.println(“Admin”);
if(ch==’F’)
System.out.println(“advisor”);
Q..5. 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!!!”);
Q.6. Rewrite the following code using for loop.
int
i=0;
while(++i<20)
{
if(i==8)
break;
System.out.println(i++);
}
Q.7.Rewrite the code using switch statement:
if(k==1)
day=”Monday”;
else
if(k==2)
day=”Tuesday”;
else
if (k==3)
day=”Wednesday”;
else
day=”-“;
}
Q8. What will be the output of :.
if (aNumber >= 0)
if (aNumber == 0)
System.out.println("first string");
else System.out.println("second
string");
System.out.println("third string");
If
aNumber is (i) 0 (ii) 2
Q9. What will be the output of the program?
int count = 1;
do
{
System.out.println("Count
is: " + count);
count++;
} while (count < 11);
Q10. What will be the output of the program
int
i=1,j=-1;
switch(i)
{
case
1,-1: j=1;
case
2 : j=2;
default
:j=0;
}
Q11. What will
be the output of the program?
int i
= 1, j = 10;
do
{
if(i > j)
{
break;
}
j--;
}
while (++i < 5);
System.out.println("i
= " + i + " and j = " + j);