OUTPUT PROGRAMS
OUTPUT PROGRAMS
Analyze the following program segment and determine how many time the loop will be
executed. What will be the output of the program segment?
int k = 1, I = 2;
while(++i<6)
k*=i;
System.out.println(k);
2. Give the output of the following program segment and also mention the number of times
the loop is executed.
int s,b;
for(a=6;b=4;a<=4;a=a+6)
{
If(a%b==0)
break;
}
System.out.println(a);
3. Give the output of the following program segment and also mention how many times the
loop is executed.
int i;
for(i=5;i>10;i++)
System.out.println(i);
System.out.println(i*4);
4. Analyse the following program segment and determine how many times the loop will be
executed and what will be the output of the program segment?
int p = 200;
while(true)
{
If(p<=100)
Break;
P=p-20;
}
System.out.println(p);
5. How many times will the following loop execute? What value will be returned?
int x = 2, y = 50;
do
{
++x;
y-=x++;
} while(x<=10);
return y;
6. What is the value of x after executing the loop? And How many times the loop gets
executed?
int x=0;
int a = 10;
int b = 7;
while(a!=0)
{
x=a+b;
a-=2;
}
System.out.println(x);
7. What is the final value of ctr after the iteration process given below executes?
int ctr=0;
for(int i =1;i<=5;i++)
for(int j=1;j<=5;j+=2)
+=ctr;
8. Write the output of the following code segment:
char ch;
int x = 97;
do
{
ch=(char)x;
System.out.print(ch+” “);
If(x%10==0)
break;
++x;
} while(x<=100);
9. Analyze the given program segment and answer the following questions:
a. Write the output of the program segment
b. How many times does the body of the loop gets executed?
for(int m=5;m<=20;m+=5)
{
if(m%3==0)
break;
else
if(m%5==0)
System.out.println(m);
continue;
}
10. Give the output of the following code and how many times the loop will execute?
int i;
int j=12;
for(i =1;i<j;i*=2)
{
System.out.println(--j);
}
11. Give the output of the following program segment and also mention the number of times
the loop is executed.
int a,b;
for(a=6,b=4;a<=24;a=a+6)
{
If(a%b==0)
break;
}
System.out.println(a);
12. How many times the following loop will gets executed? What is the output of the program?
int x=1;
do
{
System.out.println(x);
}while(x++<5);
13. Analyze the given program segment and answer the following questions:
for(int i=3;i<=4;i++){
for(int j=2;j<I;j++){
System.out.print(“ “);
}
System.out.println(“WIN”);
}
a. How many times does the inner loop execute?
b. Write the output of the program segment.
14. Give the output of the following code and mention how many times the loop will execute?
int i;
for(i=5;i>=1;i--)
{
If(i%2==1)
continue;
System.out.print(i+” “);
}
15. Give the output of the following program segment and mention how many times the loop
will execute:
int k;
for(k=5;k<=20;k+=7)
if(k%6==0)
continue;
System.out.println(k);
16. What is the output of the following code segment and how many times the inner loop gets
executed?
int x =10;
for(int i =1;i<=x;i++)
{
for(int j=5;j>=1;j--)
{
i+=3;
j-=2;
System.out.print((j*i)+” “);
}
}
17. What is the output of the following code segment and how many times the inner loop gets
executed?
int x = 5;
int n=10;
while(x<n)
{
for(int i = 1; i<x;i++)
{
System.out.println(++i * --x);
}
System.out.println(x*n);
n=n/10;
}
18. Write the output of the following code fragment and how many times the loop gets
executed?
int a =10;
int j=4;
if(a%2==0)
{
a--;
for(int i =1;i<j;i++)
{
System.out.println(a + j);
}
a=a+j;
}
System.out.println(a);
19. Rewrite the following program segment using while and do-while instead of for statement
int f=1,i;
for(i=1;i<=5;i++)
{
f*=i;
System.out.println(f);
}
20. Convert the following segment into equivalent while and do-while loop:
int a, b;
for(a=20,b=10;b>=1;b=b-2)
a++;
21. Convert the following while loop to the corresponding do-while and for loop:
int m = 5, n=10;
while(n>=1)
{
System.out.println(m*n);
n--;
}
22. Convert following do-while loop into while and for loop.
int i = 1;
int d = 5;
do{
d=d*2;
System.out.println(d);
i++; } while (i<=5);
23. Rewrite the following code segment using while and do-while loop.
int product=1;
for(int i=1;i<=100;i++)
{
if(i%2==0)
{
product=prduct*i;
}
}
System.out.println(product);
24. Rewrite the following program segment into equivalent do-while and for loop.
int i = 1;
while(i<=10)
{
System.out.print(i+” “);
i++;
}
25. Write the equivalent do-while and for loop for the following code segment.
int x= 10;
int y = 1;
int sum = 0;
while(y<=x)
{
sum=sum+y;
y++;
}
26. Rewrite the following program segment using the if-else statements instead of the ternary
operator.
String grade = (marks>=90)?”A”:(marks>=80)?“B”:”C”;
27. Rewrite the following program segment using the if..else statement.
comm=(sale>15000)?sale*5/100:0;
28. Rewrite the following using ternary operator
if(x%2==0)
System.out.print(“EVEN”);
else
System.out.print(“ODD”);
29. Rewrite the following using ternary operator:
if(bill>10000)
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;
30. Rewrite the following using ternary operator:
if(n1>n2)
r=true;
else
r = false;
31. Convert the following if else if construct into switch case:
if(ch==’c’||ch==’C’)
System.out.print(“COMPUTER”);
else if(ch==’h’ ||ch==’H’)
System.out.print(“HINDI”);
else
System.out.print(“PHYSICAL EDUCATION”);
32. Convert the following if else if construct into switch case and ternary operator:
if(var==1)
System.out.println(“Good”);
else if(var==2)
System.out.println(“Better”);
else if(var==3)
System.out.println(“Best”);
else
System.out.println(“Invalid”);
33. Rewrite the following if-else statement using ternary operator.
int a= 10;
if(a>0)
{
if(a%2==0)
{
System.out.println(“Positive Even Number”);
}
else
{
System.out.println(“Positive Odd Number”);
}
}
else
{
System.out.println(“Negative Number”);
}
34. Rewrite the following ternary operator using if-else statement.
int y=2020;
String s=y%4==0?y%100==0?y%400==0?”Leap Year”:”Not a Leap Year”:”Leap Year”:”Not a
Leap Year”;
System.out.println(s);
35. Rewrite the following switch case program using if-else and ternary operator.
int num;
switch(num)
{
case 0: System.out.println(“Zero”);
break;
case 1: System.out.println(“One”);
break;
default: System.out.print(“Other Number”);
}
36. What is the value of Math.pow(17.5,0)?
37. What is the value of Math.sqrt(81)/Math.cbrt(81)?
38. What will be the output of the following code
int x = 37.20, y=-12.31
System.out.println(Math.ceil(x)+Math.ceil(y))
39. What is the value of Math.round(Math.abs(-17.64))?
40. What will be the following function return when executed?
Math.min(-12.33,-13.01)
Math.max(-0.92,-1.02)
41. Give the output of the following Math functions:
a. Math.ceil(4.2)
b. Math.abs(-4)
42. What will be the output of the following code? double a = 3.17
System.out.println(Math.ceil(a))
System.out.println(Math.floor(a))
43. What is the value of x after executing the code fragment?
int a = 48
double x = (Math.ceil(Math.sqrt(a++)) + Math.floor(Math.sqrt(++a)))
44. What is the value of Math.abs(Math.sqrt(36)-Math.floor(12.99))
45. Give the output of the following:
Math.sqrt(Math.max(9,16))
46. Give the output of the following:
a. Math.floor(-4.7)
b. Math.ceil(3.4) + Math.pow(2,3)
47. What is the output of the following code?
a. Math.round(8.7)
b. Math.rint(8.7)
48. Give the output:
a. Math.round(-13.50)
b. Math.round(13.50)
49. What will be the output of following code?
a. Math.exp(0)
b. Math.log(1)
c. Math.log10(1000)
50. Give the output of the following program segment:
double x = 2.9, y = 2.4;
System.out.println(Math.min(Math.floor(x),y));
System.out.println(Math.max(Math.ceil(x),y));