The document contains a series of programming questions and code snippets related to conditional statements, loops, and operators in Java. It includes examples of ternary operators, switch statements, and the use of break and continue statements. Additionally, it discusses the expected outputs of various code segments based on different input values.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views
Computer Applications Home work
The document contains a series of programming questions and code snippets related to conditional statements, loops, and operators in Java. It includes examples of ternary operators, switch statements, and the use of break and continue statements. Additionally, it discusses the expected outputs of various code segments based on different input values.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
Find the output of the following program segment, when:
(i) val=500 (ii) ~— val = 1600 @ 200
int val, sum, n=550; (ii) 400
sum =n + val > 1750 ? 400 : 200; Reason-: Arithmetical operator ‘+’ has
System.out printin(sum); higher precedence then ternary operator
Rewrite the following using Ternary operator.
Miumcomerss LOU) taxes; tax = (income <= 10000) ? 0: 12;
else tax= 12;
What will be the output of the following code?
char x= ‘A’;
m=(x al)? SAPs ars
System.out printIn(“m =” + m);
m=a
What are the values of x and y when the following statements are executed?
int a= 63, b= 36; nae
boolean x = (a>b )? true : false; 36
int y=(a5 ? false : true); raeRewrite the program segment using multiple if-construct,
iffn%2—=0 && n>35 && n<67)
t ifin<67)
{ anaed;
n=n/a;
rem = n%l
)
}
The following is a segment of a program
x=1; y=l;
itta>0) @ x=2, y=0
- Gd) x=1, y=
{ x=xtl;
ry
}
What will be the value of x and y, ifn assumes a value (i) 1 and (ii) 0?
What will be the output of the following program segment?
int a=0;
ifla>O && a<20) att; a
else a--;
‘System.outprintin(a);
‘What will be the output of the following program segment?
=2,¢, 7e@4
if(@bl|al=b) (c=Ha+—b;
System.outprint(ct “ "+a+ “"+b);
‘What will be the output of the following program snippet?
=40 no output
apa. between 50 and 200
no output
iflx >= 50 && x <= 100) ‘System. out printIn(“between 50 and 100”);
else iff >= 50 && x <= 200) ‘System.out.printin(“between 50 and 200”);
else iflx >= 50 && x <= 500) ‘System.out printin(“between 50 and 500”);- Give the output of the following code fragment when (i) opn="b’ (ii) opn=’x’ (iii) opn=’a’
switch (opn)
{case ‘a’: System.out.printIn(“Platform Independent”);
break;
case ‘b’ : System.out.printIn(“Object Oriented”);
case ‘c’ : System.out.printIn(“Robust and Secure”);
break;
default : System.out.printIn(“Wrong Input”);
(i) Object Oriented
Robust and Secure
(ii) | Wrong Input
(ii) _ Platform Independent
}
|
| Ifa=5, b=6 and c=7, then what will be the value of a, b and c if the following program
snippet is executed, when (i) Num=5 (ii) Num=3
switch(Num)
{
case 2: at+; (i)
case 3: b+; (ii)
case 4: c++;| Explain the errors present in the given code fragment. Assume that variables a, b,c, dandn |
are of int type.
switch(n)
t
Break statement in case 2 must
be the last statement after d=44
| Give the output for the following: Al
int v= 0; (Nota lower case vowel
switeh (b) programming is fun
{ case‘a’ : v+=1; wt
3 break; programming is fun
case ‘e’ : v+= (iii) programming is fun
case ‘I’: v+=1; (iv) programming is fun
en, breaks ™ 1
SSR ee Not a lower case vowel
Sais programming is fun
case us ved )
System out. print (v);
default: System.out print (“Not a lower
3
System.out print (“programming is fun”)
“If the value of bis -
"ab
@ A Gi) 0 vw & ow L
"Explain the function of ey of the followit i with ‘an example:
(a) break;
(b) conti
A break state!
statement. Exect
or switch.
forti= 1
int terminates the looping (while, do-while, for) or switch
/ It will stop 1 when i=5 and come out of loop.
The continue statement forces the next iteration of the loop to take place,
skipping all statement written after continue.
Hort = 1; i<10; i++)
_/( ifliz=5) continue;
| System.out.printin(i);
}
It will take next value of i when
5 and print 1, 2, 3, 4, 6, 7, 8, 9.