Output Finding Questions in Java
Output Finding Questions:
1 Write the output :
(i) System.out.printl(“Hello”.charAt(3));
(ii) System.out.printl(“Good morning”.substring(4));
Ans:
(i) l
(ii) morning
2. Write the output of the following code :
intx , y = 0;
for(x=1;x<=5;++x)
y = x++;
--y ;
Ans: 7 4
3. Find the output of the code:
int f=1,i=2;
do
{ f*=i;
}while(++i<5);
System.out.println(f);
Ans: 24
4. What would the following code do :
Connection , statement created
String str=”select * from emp”;
Resultsetrs= stmt.executeQuery(str);
rs.last();
int r= rs.getRow();
JOptionPane.showMessageDialog(null,””+r);
Ans : if emp table has 5 records it will
display 5
5. What will be the output of the following code segment:
String firstName = "Johua ";
String lastName = "Yacomo";
String fullName = firstName + lastName;
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);
Ans: Full Name :
JohuaYacomo
6. What will be the value of j and k after execution of the
following code:
int j=10,k=12;
if(k>=j)
{k=j;
J=k;
}
Ans: 10 10
7. How many times, the following loop gets executed?
i=0;
while (i> 20)
{
//Statements
}
Ans: 0 times
8. How many times, the following loop gets executed?
i=0;
do
{
//Statements
}while (i> 20);
Ans: 1 time
9. What will be the contents of jTextield1 and jTextField2
after executing the following statement:
StringBuffer s= new StringBuffer(“Common Wealth”);
Int c=s.capacity();
s.insert(0,’E’);
s.reverse();
jTextField1.setText(“”+c);
jTextField2.setText(s.toString());
Ans:
29
htlaeWnommoCE
10. What will be the contents of jTextield after executing
the following statement:
int num=4;
num=num+1;
if(num>5)
jTextField1.setText(Integer.toString(num));
else
jTextField1.setText(Integer.toString(num*4));
Ans : 7
11. Find the output of the following code:
int First = 7;
int Second = 73;
First++;
if (First+Second> 90)
jlabel1.setText("value is 90 ");
else
jlabel1.setText("value is not 90 ");
Ans :value is not 90
12. Find the output
int Number1 = 7,Number2=8;
int Second = 73;
if (Number1>0 || Number2>5)
if (Number1>7)
jTextField1.setText("Code Worked");
else
jTextField1.setText("Code MightWork");
else
jTextField1.setText("Code will not Work");
Ans :Code MightWork
13. How many times will the following loop get executed?
x = 5;
y = 36;
while ( x <= y)
{
x+=6;
}
Ans: 6
14. What will be the content of the jTextArea1 after
executing the following code?
Int Num = 1;
do
{
jTextArea1.setText(Integer.toString(++Num) +
"\n");
Num = Num + 1;
}while(Num<=10)
Ans: 10
15. What will be the contents of jTextfield1 and jTextfield2
after executing the following code:
String s=”KENDRIYA VIDYALAYA GUNA”
jtextfield1.setText(s.length()+” “);
jtextfield2.setText(Math.round(2.34)+“”);
Ans : 23 2
16. What will be the value of s after executing the
following code?
double i,sum=2
for(i=3;i<8;++i)
{ if(i%4= =0)
{ break;
sum=Math.pow(sum,i);
}
else
sum+=i/2;
}
Ans: 150.0625
17. What will be the content of jTextField1 and jTextField2
after executing the following code:
String st=”New to
Information Technology”;
jTextField1.setText(st.replace(“Technology”,”Practices”);
jTextField2.setText(st.substring(7));
Ans: New to Information Practices
Information Technology
18. Predict the output for tan & tan1 if sac equals 7?
int tan = 0, tan1 = 4 ;
if ( sac == 2 )
{ tan = 4 ; tan1 = 0; }
else if (sac == 8)
{ tan = 0 ; tan1 = 4; }
JOptionPane.showMessageDialog( null , “ tan = “ + tan +” ,
tan1 = “ + tan1 ) ;
Ans: tan = 0 tan1=4
19. Give the output for the following code fragment:
v = 20 ;
do
{
JOptionPane.showMessageDialog( null , v + “ ” ) ;
} while ( v< 50 ) ;
JOptionPane.showMessageDialog( null , “ Bye “ ) ;
Ans: Infinite loop
20. Give the value of x after executing following Java code.
Also find how many times the
following loop will execute? :
int a=10;
int b=12;
int x=5;
int y=6;
while (a<=b)
{ if (a%2= =0)
x=x + y;
else
x=x-y;
a=a+1;
}
Ans:11
21. What will be the output produced by following code
fragment?
flaot x=9;
float y=5;
int z=(int)(x/y);
switch(z)
{
case1:x=x+2;
case2: x=x+3;
default:x =x+1;
}
System.out.println(“value of x:”+x);
Ans: 15
22. Predict the output of the following code fragments:
inti,j,n;
n=0;i=1;
do
{ n++; i++;
}
while(i<=5);
Ans: 5
23. What will be the output of the following program code
when the user will press JButton:
Public class svm
{ int a;
svm(int p)
{ a=p; }
void assign(int no)
{ a=no; }
intdisp()
{ return a; }
}
private void
jButton1ActionPerformed(java.awt.event.ActionEventevt)
{
svm os=new svm(15);
System.out.println(“ “ + os.disp());
os.assign(35);
System.out.println(“ “ + os.disp());
}
Ans: 15 35
24. What will be the contents of jTextField1 and jTextField2
after executing the following code:
String s = “Sun Micro Systems”;
jTextField1.setText(s.length()+””);
jTextField2.setText(s.toLowerCase());
Ans:
jTextField1 : 17
jTextField2 : abc micro systems
25. What values will be assigned to the variable ua ,ub, uc
and fail after execution of the following program
segment:
int i=0,ua=0,ub=0,uc=0,fail=0;
while(i<=5) {
switch ( i++ )
{ case 1 :++ua;
case 2 : ++ub; uc++;break;
case 3 :
case 4 : ++uc; ua++;ub++;break;
default : ++fail;
}
Ans: ua=1 ub=1 uc=0
26. Predict an output of the following code fragments:
int i = 1, j = 0, n = 0;
while(i<4) {
for(j=1;j<= i ; j++)
{ n+= 1;
i = i+1;
}
System.out.println(n);
}
Ans : 6
27. Give the output of the following code:
int m=100;
while(m>0)
{
if (m<10)
break;
m=m-10;
}
System.out.println(“m is “+m);
Ans: 0
28 What is the output
of each of the following code fragments?
(given the declaration int a=1, b=2, c=3;)
1. if (6 < 2 * 5)
System.out.print("Hello");
System.out.print(" There");
Ans: Hello There
2. if(a>b)
if(a>c)
System.out.println("1111");
else
System.out.println("2222");
Ans: . No output
3. if (a < c)
System.out.println("*");
else if (a == b)
System.out.println("&");
else
System.out.println("$");
Ans: *
4. if(a<b)
System.out.println("####");
else
System.out.println("&&&&");
System.out.println("****");
Ans: ####
****
5. if(a>b)
System.out.println("####");
else
{System.out.println("&&&&");
System.out.println("****");}
Ans:
&&&&
****
6. int x = 100; int y = 200;
if (x > 100 && y <=200)
System.out.print(x+" "+y+" "+(x+y));
else
System.out.print(x+" "+y+" "+(2*x-y));
Ans: 100 200 0
7. if (a < c)
System.out.println("*");
else if (a == c)
System.out.println("&");
else
System.out.println("$");
Ans: *
8. if(++a > b++ || a-- > 0)
c++;
else
c--;
System.out.println(a+" "+b+" "+c);
Ans: 1 3 4
9. if(a<b){
System.out.println("####");
System.out.println("****");
}
else
System.out.println("&&&&");
Ans: ####
****
10.if ('a' > 'b' || 66 > (int)('A'))
System.out.println("#*#");
Ans:
#*#
29. Find the output of the following code:
(a) int I =1;
while(I<5) {
System.out.print( I+ “ ” );
I=1*2;
}
(b) int total=0,sum=0;
for(int I=0;I>=10;I++)
sum += I;
System.out.println(total);
(c) int I =0;
while(I<10) {
if( I % 2 ) = = 0)
{
x = x+ I:
System.out.print(x + “ ” );
}I++;}
(d) int I =0;
for(I=1;I<=20;I++){
System.out.print(i + “ ” );
I =I+2:
}
Ans: a) 1 2 4 b) 0 c) 0 2 6 12 20 d) 1 4 7 10
13 16 19
e) int I =0,x = 0:
do{
if (I% 5 = = 0){
x ++:
System.out.print(x + “ ” );
}
++i:
}
while(I<20);
System.out.print(“\n + x );
Ans:1
2 3 4
4
f) int I =0,x = 0;
for (I=0;I<5;++I)
for (I=0;j<i;j++) {
x += (I+j1);
System.out.print(i + “ ” );}
System.out.print(“\n +x );}
Ans:0
1 3 5 8 12 15 19 24 30
30
g) int I =0,x = 0;
for (I=1;I<10;I* = 2){
x ++:
System.out.print( x + “ ” );
}
System.out.print( “\n” + x);
Ans: 1 2 3 4
4
30. Predict
the output of the following code fragments.
(a)
float x =9;
float
y = 5;
int
z = (int) (x/y);
switch
(z) {
case
1: x= x + 2;
case
2: x= x + 3;
default
: x = x+1;
}System.out.println(“Value
of x :” + x);
(b)
int i,j,n;
n=0,
i= 1;
do
{
n++;
i++;
}
while(i<=5);
(c)
int i =1, j = 0 , n = 0;
while
(i<4) {
for
(j=1; j<=i; j++) {
n
+=1;
}i
= i + 1;
}
System.out.println(n);
(d)
int j=1, s=0;
while(j<10)
{
System.out.println(J+
“+”);
s
= s +j;
j
= j + j % 3;
}
System.out.println(“=” + s);
Ans: a) x = 15, b) No output
c) 6 d) 1+2+4+5+7+8= 27
31.
What will be the content of jTextField1 and
jTextField2 after executing the following code?
String st="Happy New Year
2012";
jTextField1.setText(""+st.substring(17)+st.length());
jTextField2.setText(st.substring(6).trim());
Ans: jTextField1 will
contain 1219 and jTextField2 will contain
New Year 2012
332.
Predict the output:
float f4 = -5.5;
float f5 = 5.5;
float f6 = -5.49;
float f7 = 5.49;
System.out.println("Round f4 is " +
Math.round(f4));
System.out.println("Round f5 is " +
Math.round(f5));
System.out.println("Round f6 is " +
Math.round(f6));
System.out.println("Round f7 is " +
Math.round(f7));
}
Ans: Round f4 is -5
Round f5 is 6
Round f6 is -5
Round f7 is 5