< <
Vidya Pratishthan’s
Vinodkumar Gujar Bal Vikas Mandir, Baramati.
Std.: IX (A & B) Computer Applications
============================ ============== =
Assignment No. : 4
1) State the output of the following code -
int m= 5, n =10;
if(m > 5)
{
if( n > 10)
System.out.println(“m is= "+m);
}
else
System.out.println(“n is= "+n);
2) State the output of the following code -
int m= 5, n =10;
if(m > 5)
if( n > 10)
{
System.out.println(“m is= "+m);
}
else
System.out.println(“n is= "+n);
3) What’s the problem with the following code -
int a = 5, b = 10;
if((a > b) || (a=10))
System.out.println("Ok”);
else
System.out.println("Not Ok”);
4) State the error in the following code -
int m = 5, n== 10;
if(m => 5)
{
if(n > 10)
System.out.println("m is "+m);
}
else
System.out.println(“n is " +n);
..2
-2–
5) Correct the following code
if(x = 1)
y = 100;
else
y == 200;
6) State the output of the following code for the values of m as-
i) m = 2
ii) m = 4
iii) m = 1
iv) m = 3
if( m == 1)
System.out.println("My value is 1”);
else if( m == 2)
System.out.println("My value is 2”);
else if( m == 3)
System.out.println("My value is 3”);
else
System.out.println("I am out of range ”);
7) State the output of the following code for the values of m as-
i) a = 2 , b = 6
ii) a = 8 , b = 0
iii) a = 3 , b = 4
if( a > 3)
{
if(b <= 6)
System.out.println("Yes”);
else
System.out.println("No”);
}
else
{
if(b == 6)
System.out.println("True”);
else
System.out.println("False”);
}
8) Write a statement using conditional/ternary operators to convert the
value of a variable d into absolute form.
..3
-3–
9) Write a conditional statement to assign and print grades as ‘P’ or ‘F’
when the percentage marks is greater than or equal to 250.
10) Give the output of the following code if, n= 10 and m = 30?
int y = n >= 7 ? m * 10 : m * 5 ;
System.out println("The final y = "+ y);
11) Correct the following code and re-write the correct code.
int f = m = 7 : m ** 4 ? m - 5;
12) What syntax error is there in the following code?
int x = 2, nl, n2, n3, n4;
switch(x)
{
case 1: nl = 10;
n2 = 20;
case 2: n4 = 40;
break;
n3 = 30;
}
13) Write a conditional statement using ternary operators to store true or
false as the result if value or variable ‘x’ is smaller than or equal to ‘y’.
Also print the final result.
14) Consider the following code, if d = 10 :
switch (n)
{
case 1: System.out.print(++d ); break;
case 2: System.out.print( d++ ); break;
case 3: System.out.print( (++d + -- d) ); break;
case 4: System.out.print( ( d-- - --d ) ); break;
default: System.out.print("Empty ");
}
What will be the output of the above code, if value of n is
(a) 2 (b) 3 (c) 4 (d) 1 (e) 5
..4
-4–
15) Write a statement using ternary operators to assign and print grades
as ‘P’ or ‘F’ when the percentage marks is greater than or equal to 250.
16) Consider the following code -
switch (ss)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’: System.out.print("Vowel ”);break;
default: System.out.print("Consonant ");
}
What will be the output of the above code, if value of ss is character
literal as -
(a) ‘E’ (b) ‘x’ (c) ‘i’ (d) ‘M’ (e) ‘U’ (f) ‘g’ (g) ‘L’
17) Consider the following code -
switch (n)
{
case 1: System.out.print(“ONE”);
case 2: System.out.print(“TWO”);
case 3: System.out.print(“THREE”); break;
case 4: System.out.print(“FOUR”); break;
default: System.out.print("Wrong Choice ");
}
What will be the output of the above code, if value of n is -
(a) 2 (b) 3 (c) 4 (d) 1 (e) 5 (f) -7
..5
-5–
18) What will be the output of the following code -
if (st == ‘E’|| st == ‘A’|| st == ‘I’|| st == ‘U’|| st == ‘O’
System.out.println("An uppercase vowel ");
else if (st == ‘e’|| st == ‘a’|| st == ‘i’|| st == ‘u’|| st == ‘o’
System.out.println("An lowercase vowel ");
else if (st>= ‘a’ && st<= ‘z’)
System.out.println("A lowercase consonant ");
else
System.out.println("An uppercase consonant ");
What will be the output of the above code, if value of st is -
(a) ‘E’ (b) ‘x’ (c) ‘i’ (d) ‘M’ (e) ‘L’
19) Rewrite the following code using the if-else statement:
int ch = ( m>300 ) ? ( m/10 ) * 2 : ( m /20) - 2;
20) Rewrite the following code using the switch-case statement:
if(op == ‘+’)
z= m + n;
else if if(op == ‘-’)
z= m – n;
else if if(op == ‘/’)
z= m / n;
else if if(op == ‘*’)
z= m* n;
else
System.out.println("OOPS!! Wrong choice );
*******