CH 3 ASSIGNMENTS

Output Finding Questions:
1 Write the output :
(i) System.out.printl(“Hello”.charAt(3));
(ii) System.out.printl(“Good morning”.substring(5));

2. Write the output of the following
code : int x , y = 0;
for(x=1;x<=5;++x)
y = x++;
--y ;

3. Find the output of the code: 
int f=1,i=2;
do
{ f*=i;
}while(++i<5);
System.out.println(f);

4. What would the following code do :
Connection con;
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);

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;
}

7. How many times, the following loop gets
executed?
 i=0;
while (i> 20)
{
//Statements
}

8. How many times, the following loop gets executed?
i=0;
do
{
//Statements
}while (i> 20);


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));

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 ");

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");

13. How many times will the following loop get executed?
    x = 5;
    y = 36;
    while ( x <= y)
         {
            x += 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);

15.What will be the contents of jTextfield1 and jTextfield2 after executing the following code:
String s=”KENDRIYA VIDYALAYA NO.2 GOLCONDA”
jtextfield1.setText(s.length()+” “);
jtextfield2.setText(Math.round(2.34)+“”);

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;
}

17. What will be the content of jTextField1 and jTextField2 after executing the following code:
 String st=”Newto Information Technology”;
jTextField1.setText(st.replace(“Technology”,”Practices”);
jTextField2.setText(st.substring(7));

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 ) ;

19. Give the output for the following code fragment:
v = 20 ;
do
{
JOptionPane.showMessageDialog( null , v + “ ” ) ;
} while ( v< 50 ) ;
JOptionPane.showMessageDialog( null , “ Bye“ ) ;








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;
}

21. What will be the output produced by following codefragment?
float 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 ofx:”+x);

22. Predict the output of the following code fragments:
 int i,j,n;
n=0;i=1;
do
{ n++; i++;
}
while(i<=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; }
}
privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt)
{svm os=new svm(15); System.out.println(“ “ + os.disp()); os.assign(35);
System.out.println(“ “ + os.disp());
}


24.What will be the contents of jTextField1 and jTextField2 after executing the following
code:
String s = “SunMicro Systems”;
 jTextField1.setText(s.length()+””);
jTextField2.setText(s.toLowerCase());

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;
}

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);
}

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);

No comments:

Post a Comment