Output based Questions worksheet 1
1. x = 5; y = 50;
while(x<=y)
{
y = y / x;
System.out.println(y);
}
2.
int m=2;
int n=15;
for(int i=1;i<5;i++)
m++;
--n;
System.out.println("m="+m);
System.out.println("n="+n);
3. int a,b;
for(a=6,b=4; a <= 4; a=a+ 6)
{
if(a%b==0)
break;
}
System.out.println(a);
4. for(int m=5;m<=20;m+=5)
if(m%3==0)
break;
else
if(m%5==0)
System.out.println(m);
continue;
5. int a=0;
while(a> - 5)
System.out.print(a+””);
System.out.print(--a*2);
System.out.println();
--a;
6.
int k,j;
for(j=1;j<=5;j+=2)
{
for(k=1;k<=j ;k++)
System.out.print(k);
System.out.println();
7.
int j, k, p=-1;
for(j= - 2;j<=1;j++)
for(k=j; k<=0;k++)
k=Math.max(j*k, p)
System.out.print(k);
p=p+2;
i. How many times Outer loop runs? What is the output?
8.int k=5,j;
while(k>=1)
j=1;
while(j<=k)
System.out.print(j);
j++;
System.out.println();
k--;
9. int i,j;
first:
for(i=1;i<10;i+=2)
for(j=3;j>=1;j=j-2)
if(i>5)
break first;
System.out.println(“i=”+i+”j=”=j);
10.
int i,j;
first:
for(i=0;i<5;i=i+2)
second:
for(j=1;j<=10;j++)
System.out.println(“i+””+i+”j”+j)
if(J==2)
break second;
11. int a[]=new int [5];
a[0]=4; a[1]=8; a[2]=7; a[3]=12; a[4]=3;
System.out.println(a[2+1]);
12. String str = "Computer Applications" + 1 + 0;
System.out.println("Understanding" + str);
13. String n1 = "46", n2 = "64";
int total = Integer.parseInt(n1) + Integer.parseInt(n2);
System.out.println("The sum of " + "46 " + "and" + " 64" + " is " + total);
14. If:
String x = "Computer";
String y = "Applications";
What do the following functions return?(i)
System.out.println(x.substring(1,5));
15. String n = "Computer Knowledge";
String m = "Computer Applications";
System.out.println(n.substring(0,8).concat(m.substring(9)));
System.out.println(n.endsWith("e"));
16. String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW",
"JAIPUR"};
System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0,3));
17. i.Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type.
18. char ch= ‘F’;
int m= ch;
m=m+5;
System.out.println(m+ ” ” +ch);
19. String s= “Today is Test”;
System.out.println(s.indexOf(‘T’));
System.out.println(s.substring(0, 7) + ” ” + “Holiday”);
20.
int x = 5;
x-=x++*2+3* – -x;
21.
char x=‘A’; int m;
m= (x==’a’) ? ‘A’: ‘a’;
System.out.println(“m=” +m);
22. What is the value stored in variable res given below :
double res = Math.pow (“345”.indexOf(‘5’), 3);
23. What are the values of a and b after the following function is executed, if
the values passed are 30 and 50:
void paws(int a, int b)
{
a=a + b;
b=a – b;
a=a – b;
System.out.println (a+ “,” +b);
}
24. State the output when the following program segment is executed:
String a =“Smartphone”, b=“Graphic Art”;
String h=a.substring(2, 5);
String k=b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
25. char ch ;
int x=97;
do
{
ch=(char) x;
System.out.print(ch + “ ” );
if(x%10 == 0)
break;
++x;
}while(x<=100);