Logical Questions (Ouput and Conversion)
Logical Questions (Ouput and Conversion)
Exercise 1
1. Find the output of the following program segments, when:
(a) val = 500
(b) val = 1600
int n = 550, sum;
sum = n + val > 1750 ? 400 : 200;
System.out.println(sum);
Ans: (a) 200
(b) 400
2. What is the value of
Math.round(Math.abs(-19.70))?
Ans: 20
3. What is the output of the following?
char c = 'A';
short m = 26;
int n = c + m;
System.out.println(n);
Ans: 65 + 26 //ASCII of A is 65
= 91
5. Give the output of the following expressions:
If x = - 9.99, calculate Math.abs(x);
6. Give the output of the following expressions:
If x = 9.0, calculate Math.sqrt(x);
Ans: (5) 9.99
(6) 3.0
6. What will the following functions return when executed?
(a) Math.max(-17, -19);
(b) Math.ceil(7.8);
1
Ans: (a) -17
(b) 8
7. What will ne tje output of the following code?
float x = 7.87;
System.out.println(Math.ceil(x));
System.out.println(Math.floor(x));
Ans: 8.0
7.0
8. Write the statements for the following:
(i) To create an object of a class Student
(ii) To invoke a method study() of the above class.
Ans: (i) Student ob = new Student();
(ii) ob.study();
9. Rewrite the following using the Ternary operator:
if(a > b)
c = a;
else
c = b;
Ans: c = a > b ? a : b;
10. When in x = 35, what will be the output of the following?
(i) Math.ceil(Math.sqrt(x++));
(ii) Math.floor(Math.sqrt(++x));
Ans: (i) 6.0
(ii) 6.0
11. Evaluate: Math.ceil(7.8) + Math.round(6.3)
Ans: 8.0 + 6.0
= 14.0
12. What will be the value of floor(7.9) - ceil(7.9)
Ans: -1.0
13. Evaluate: Math.sqrt(Math.floor(16.95))
Ans: 4.0
2
14. Use switch case statement for the given program snippet:
if(a==100)
c=a*2;
if(a==200)
c=a*4;
if(a==600)
c=a*10;
Ans: switch(a)
{
case 100:
c=a*2;
break;
case 200:
c=a*4;
break;
case 600:
c=a*6;
break;
}
15. Use if else statement for the given snippet:
String st=(a>0)?(a%2==0)? "Positive even" : "Positive odd" : "Negative number";
Ans: String st; int a;
if(a>0)
{
if(a%2==0)
st="Positive even";
else
st="Positive odd";
}
else
st="Negative number";
3
Exercise 2:
1. consider the following code. rewrite the code using i) do while loop ii) for loop
int amount =100;
while(amount>0)
{
System.out.println(amount);
amount=amount-1;
}
3. Rewrite the following for loops by using while and do-while loops:
for(I = 1; j = 1; I <= 10; i++; j++)
{
System.out.println(I * j);
}
4. int p = 20;
for(k = p; k >= 0; k -= 2)
{
s += k;
}
System.out.println(“Sum =” + s);
int m = 5, n = 10;
while(n >= 1)
{
System.out.println(m * n);
n--;
4
}
Exercise 3:
1. What is the final value of ctr when the iteration process given below executes?
int count =0;
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j+=2)
++count;
2. 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);
}
3. Give the output of the following program segment and also mention how many
times the loop is executed:
for ( int i = 5 ; i > 10; i ++ )
System.out.println( i );
System.out.println( i * 4 );
4. Analyze the given program segment and answer the following questions:
for(int m = 5; m <= 20; m+=5)
{
if(m % 3 == 0)
break;
else if(m % 5 == 0)
System.out.println(m);
continue;
}
(i)Write the output of the program segment.
(ii) How many times does the body of the loop gets executed?
5. Analyse the following program segment and determine how many times the body
of the loopwill be executed (show the working).
5
int x = 5, y = 50;
while(x <= y)
{
y = y / x;
System.out.println(y);
}
7. 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 <= 4; a = a + 6)
{
if(a % b == 0)
break;
}
System.out.println(a);
8. How many times will the following loop execute? What value will be returned?
int x=2;
int y=50;
do
{
++x; y-=x++;
}
while(x<=10);
return y;