Wednesday, 29 August 2012

Output Questions - JavaNetbeans


Identify the possible error(s) in the following code fragment: Underline error(s) and
correct the code.
f=1;
for(int a=40; (>30); a)
f*=a;
s=0;
for(int a=1; a<40/a++)
s+=a;
Ans: Error in the first and second for loop line ,and the corrected code should be as follows:
for ( int a =40 ; (a >30); a )
for ( int a =1 ; (a <40); a ++ )

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

Rewrite the following fragment using switch:
if ( ch == ‘E’)
eastern ++;
if ( ch == ‘W’)
western ++;
if (ch ==’N’)
northern++;
if (ch == ‘S’)
southern++;
else
unknown++;

Ans: switch(ch) {
case ‘E’ : eastern ++; break;
case ‘W’ : western ++; break;
case ‘N’ : northern ++; break;
case ‘S’ : southern ++; break;
default : unknown++;}

Given the code fragment:
i = 2;
do { System.out.println(i);
i+=2;
}while(i<51);
jOptionPane.showMessageDialog(null, “Thank you”);
Rewrite the above code using a while loop.
Ans: i = 2;
while(i<51) { System.out.println(i);
i+=2;
}
jOptionPane.showMessageDialog(null, “Thank you”);

Rewrite following while loop into a for loop
int stripes = 0;
while ( stripes <=13) {
if ( stripes % 2 == 2)
{ System.out.println(“Colours code Red”);
}
else { System.out.println(“Colours code Blue”);
}
System.out.println(“New Stripe”);
stripes = stripes + 1;
}
Ans: for ( int stripes =0; stripes <=13; stripes++)
{ if stripes % 2 == 2)
{ System.out.println(“Colours code Red”);
}
else { System.out.println(“Colours code Blue”);
}
System.out.println(“New Stripe”);
}

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
Find the output:
a)int f=1,i=2;
do
{
f*=i;
} while(++I <5);
System.out.println(f);
b) int I=0;
label :
if(I<2)
{
system.out.print(“I is “ +I);
I++;
continue label;
} 2

What will be the output the following code fragment if the value of ch is
(i) a (ii) c (iii) d (iv) b?
switch (ch)
{ case ‘a’: system.out.println(“It is a.”);
case ‘b’: system.out.println(“It is b.”);
case ‘c’: system.out.println(“It is c.”);
break;
case ‘d’: system.out.println(“It is d.”);
break;
default: system.out.println(“Not a ,b,c ,and d.”);}
What will be the output of the following code snippet?
int x= 10;
int y = 20;
if ((x<y)||(x=5) > 10)
System.out.println(x);
else
System.out.println(y);

Find the syntax errors if any in the following programme:
int i ; sum=0;
i=1;
while(i=<10)
{
sum=sum+i;
i=i+3
}
System.println(sum);

Rewrite the following switch statement using if-else statement and display the result
using appropriate IDE.
switch(number){
case 1:
jLable2.setText(“digits”);
break;
case 10:
jLable2.setText(“Tens”);
break;
case 100:
jLable2.setText(“Hundreds”);
break;
case 1000:
jLable2.setText(“Thousands”);
break;
default:
jLable2.setText(“error”);
break;
}

Find the output of the following code fragments ?
(a)   int s = 14; (b) int s = 14;

a)      if(s<20)                                               =
System.out.print(“Under”);               
Else                                                    
System.out.print(“Over”);                  
System.out.print ln(“the limit”);        

b) If (s < 20)
System.out.print(“Under”);
else {
System.out.print(“Over”);
System.out.print ln(“the limit”);}

 (c) int s = 94;
If (s < 20) {
System.out.print(“Under”);
}
else {
System.out.print(“Over”);
}
System.out.print ln(“the limit”);

What will be the output of the following code fragment when the value of ch is
(a) ‘A’ (b) ‘B’ (c) ‘D’ (d) ‘F’
switch (ch) {
case ‘A’ : System.out.print ln (“Grade A”);
case ‘B’ : System.out.print ln (“Grade B”);
case ‘C’ : System.out.print ln (“Grade C”);
break;
case ‘D’ : System.out.print ln (“Grade D”);
default : System.out.print ln (“Grade F”);
}




e) The following code has some error(s). Rewrite the correct code underlining all the corrections made.
       int i,c,a=5,b=0;
       for(i=0,i<20,i++)
          { if b=0 then
                  break;
              else
                   c=a/b;
           system.out.show(“Quotient”+c);    
Ans:  int i,c,a=5,b=0;
          for(i=0;i<20;i++) // at place of comma(,) semicolon (;) should be there
          { if (b==0)          // comparison operator = = should be used and brackets are missing.
                  break;
              else
                   c=a/b;
           System..out.Println(“Quotient”+c);
              
f) 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
What would be output of the following code segment written in java.
public static void main(String [] args)
{  int a=10, b= 9;
    boolean x;
    x = a++ == ++b;
    System.out.print("x = "+x); }

Find the output of the following code snippet written in java

public static void main(String []args)
{
    int x=10, y=5;
    System.out.println(x++);
    System.out.println(++y);
    System.out.println((x++ + ++y));
    System.out.println((++x - y++));
    System.out.println((x++) + (++y));
    System.out.println((++x)-(y++));
    System.out.println((x++) + (++x));
    System.out.println("x = "+ X +" y="+ y);
}

Find the output of the following code snippet written in java
public static void main(String []args)
{
    long a=78345,s1=0,s2=0,r;
    while(a>0)
    {
        r=a%10;
        if (r%4==0)
            s1+= r;
        else
            s2+=r;
        a/=10;
    }
    System.out.println("S1 ="+ s1);
    System.out.println("S2 ="+ s2);
}

Find the output of the following code snippet written in java

public static void main(String args[])
{
    int no1=2,no2=1,i;
    while(no2<=3)
    {
        i=no1;
        while(i<=no2)
        {
            no1=no1*i;
            i++;
        }
        System.out.println("No1 "+ no1);
        System.out.println("No2 "+ no2);
        no2++;    
    }
}

Find the output of the following code snippet written in java
    public static void main(String args[])
{
        int x=1,y=1,z=1,i=1;
        while(y<=3)
        {
            z=1;
            while(z<=y)
            {  x=x*i;
                i++; z++;        
            }
            System.out.println("x= "+x);
            y++; } }

Find the output of the following code segment written in JAVA. Assume all necessary header files have been included and ignore any syntax error if any.

public static void main(String [] args)
{
int i = 3, a=0, b=1, n=5, c;
System.out.print(a+ “ “+b+” “);
while ( i < n)
{ c= a + b;
System.out.print(c+” “);
a = c - a;
b = c;
i++; } }

How many times will the following loop get executed and what will be the final value of the variable I after execution the loop is over.

int I = 5;
do
{ I += 3;
    System.out.println("I = "+I);
    I = I + 1; }
while (I >= 9);

Correct the errors in the following program segment written in JAVA. You are just required to write the corrected code, underlying the corrections made.

Public Static Void Main (String [] args)
{
    Integer Nos = 100;
    while (Nos => 45)
    {
        If (Nos % 5 = 0);
            Nos+=10;
        otherwise
            Nos + = 20;     } }

Convert the following ‘while loop’ into its equivalent ‘for loop without altering the output of the program.
int x = 100, a = 30;
while ( x>= 10)
{
    System.out.println(”New Amount =”+(x + a));
    a++;
    x - = a;
}
Predict the output:
a)      String name=”TATA”;
int i=0;
While(i<name.length())
{
System.out.println(name.substring(I,i+1));
i++;
}
b)      int a=1;
int count=20;
do
{
a=a*count;
count=count-2;
} while(count<10);
system.out.print(a);

c)       Given a string object names salary having value as ‘55000” stored in it ., Obtain the output of the following:
JoptionPane.showMessageDialog(null,+salary.length()+Integer.parseInt(salary));

d)      String a=”abc”;
System.out.println(a+”abc”+”12”);

e)      Int a =5,b=10;
If(a>5)
If(b>5)
System.out.print(b);
Else
System.out.print(a);
System.out.print(“notvalid”);

f)       for(int a= 40;a>10;a--)
Int f=f*a;
System.out.print(f);

g)      jTextArea1.setText(“India\nIncredible\n\tIndia”);
h)      int x=20;
int y=10;
int z=0;
if((++y<x--)||(y!=5))
{
System.out.println(x++);
System.out.println(y);
Z=++y+ ++x;
System.out.println(z)
System.out.println(x);}

Predict the output:
For(int i=1;i<4;i++)
{
For(int j=1;j<I;j++)
System.out.print(i+” “);
System.out.println();}

Predict the output:
a)Int val1-5,val2=10;
For(int i=1;i<=3;i++)
{
System.out.println(val1++ + “,”+ --val2);
System.out.println(val2--+ “,” + ++val1);
}

b)Int i=10,j=3;
Switch(i/%j)
{
Case 0: System.out.print(“hello”);
Break;
Case 1: system.out.print(“welcome”);
Break;
Case2: system.out.print(“not welcome”);
Break;
}

Convert the following switch  into if else

Int j;
J=integer.parseInt(jTextField1.getText());
Switch(j)
{
Case 100: j=j*j;
Case 200: j=j+j;
Break;
Case 300: j=j-j;
Case 400:j=j+2;
Break;
Default:j=j+8;
}


Predict output:
Public static void main(string[] args)
{
String fullname=”bill gates”;
String firstname =””;
Firstname=fullname.substring(0,2);
System.out.print(firstname);
Firstname= FullName.substring(5, 2);
System.out.print(firstname);
Firstname= FullName.substring( 5, FullName.length( ) - 3 );
System.out.print(firstname);
}

Predict output:

String s1 = "Amit";
String s2 = "Amit";
    String s3 = abcd";
    String s4 = abcd";
    System.out.println(s1.equals(s2));
    System.out.println((s1==s2));
    System.out.println(s3.equals(s4));
    System.out.println((s3==s4));

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

What will be the values of x and y after execution of the following code:                      
int x,y=0;
for(x=1;x<=5;++x)
y=x++ + ++x;
--y;
Give the output of the following statements.                                                                      
jTextField1.setText(Integer.parseInt(“21”+100)+100);
jTextField1.setText(“CBSE\nFinal\tExam”);

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; } }
The following code has some error(s).Rewrite the correct code underlining all the       corrections made:                                                                                                     
int SUM=0,Step=5;
int I;
For(i=0;i=<5;++i)
{ Step+=5;
Sum+=Step;}
jTextField1.showText(Double.toString(SUM));


What will be the value of P and Q after execution of the following code?         
                        int P,Q = 100
                        for (P=10; P<12; P++)
                        {
                             Q+ = P;
                        }
                        JOptionPane. showMessageDialog(this, “p + “+P+” Q;”+Q+” );
What will be the constants of jTextField1 and jTextField2 after executing the following code:          
 i)  jTextField1.setText(Math.round(25.7)+ “ ”);       
 ii) jTextField2.setText(Math.pow(2,3)+ “ ”);           
What will be the contents of F1 and F2 after the following code is executed?   
     String  F1=”hello”, F2=”Friend”;
      F1=F1.concat(F2);


Given the following code fragment :Write an alternative code using switch case.                        
If(a==0)
{
If(a==1)

System.out.println(“zero”);
System.out.println(“one”);
}
If(a==2)
System.out.println(“two”);
If(a==3)
System.out.println(“three”);


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


       What will be an output of the following code:                                                            
                        int i,j,n;


                        n=0;     i=1;
                        do
                        {
                        n++;     i++;
                        System.out.println(n+”    “+i);
                        }while(i<=5);


 Predict the output of following code fragments:
(a) int i, j, n;                                                                             (b) int i=1, j=0, n=0;
n=0; i=1;                                                                                  while (i<4) {
do                                                                                            { for(j=1; j<=i; j++) {
n++; i++;                                                                                             n+=1;
} while (i<=5);                                                                                                }
i=i+1;
}
                                                 System.out.print ln(n);
(c) int i=3, n=0;                                                                       (d) int j=1, s=0;
while (i<4) {                                                                            while(j<10) {
n++; i--;                                                                                   System.out.print(j+ “+”);
}                                                                                              s=s+j;
System.out.print ln(n);                                                                       j=j+j%3;}
System.out.print ln(“=”+s);
Find out errors if any;
(a) m=1;                                                                                   (b) while(ctr !=10) ; {
n=0;                                                                                                     ctr=1;
for(;m+n<19;++n)                                                                               sum=sum + a;
System.out.print ln(“Hello \n”);                                                                      ctr=ctr + 1;
m=m+10;                                                                                                         }


Find errors in the following code :
Import java.sql* ;
:connection conn =
DriveManager.getconnention(“jdbc:mysql://localhost/test”,”root”,”wxyz);
Statement stmt = con.createstatement();
Sql= “SELECT empno,emame,job,sal From EMPL; “ ;
Resultset rs = stmt.executeQuery(sql); }
Catch(Exception e) { }
What will be the output after executing the following code?
int fun(int n)
        { int r=(n%2==0)?1:0;
           return r;       }     
int t=5,p;
        do
        {   p=fun(t);
             if(p==1)
                 System.out.println(t+p);
             else
                  System.out.print(t+p);
        }while(++t<10);