Java Exercises for you

JAVA Exercises for you 

1 1.        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); }
  
2   2.     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);
}

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

4   4.        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++;    
    }
}

5  5.        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++; } }

6  6.       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++; } }

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

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

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

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

1  11.   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;
}

1   12.   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;
}


1   13.   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);
}

1 14.   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));



1 15.   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;
16. Find  the output of the following statements.                                                                      
jTextField1.setText(Integer.parseInt(“21”+100)+100);
jTextField1.setText(“CBSE\nFinal\tExam”);

1  16.   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; } }
1 17.   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));


1  18.   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+” );
19. 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)+ “ ”);     
    
20.   What will be the contents of F1 and F2 after the following code is executed?  
     String  F1=”hello”, F2=”Friend”;
      F1=F1.concat(F2);

21.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”);


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


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


 24.Predict the output of following code fragments:
(a) int i, j, n;                                                                            
n=0; i=1;                                                                                 
do                                                                                            
n++; i++;                                                                                        
} while (i<=5);                                                                                                
i=i+1;
}      System.out.print ln(n);

(b) int i1,j=0,n=0;
While(i<4)
{
for(j=1;j<=I;j++)
{
n+=1;       
}
 System.out.print ln(n);
(c) int i=3, n=0;                                                                       
while (i<4) {                                                                            
n++; i--;                                                                                                                                                                              
System.out.print ln(n);                                                                       
 (d) int j=1,s=0;
While(j<10)
{
System.out.print(j+ “+”);
s=s+j;
j=j+j%3;}
System.out.print ln(“=”+s);

25. Find out errors if any;
(a) m=1;                                                                                   
  n=0;                                                                                                                                                             sum=sum + a;
System.out.print ln(“Hello \n”);                                                                     
m=m+10;                                                                                                         
(b) while(ctr !=10) ; {
ctr=1;
for(;m+n<19;++n)                                                                              
ctr=ctr + 1;
}

26. 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) { }

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


28. The following code has error(s). Rewrite the correct code underlining all the corrections made
int Sum = 1 , Step = 5 ;
int i ;
for ( i = 0 ; i < = 5 , i ++) ;
{
 Step += 5 ,
 Sum += Step ;
}
jTextArea1 . showText ( " " + Sum )

29.  Find out errors if any:
int i; sum= 0;
 i=1 ;
while (i<=5)
{
sum =sum +i
i+=3
} System.out.println(sum) ;

30.Predict the output:
 a. Predict the output of the following code fragments:
int val1 =5; val2=10;
for(int i=1;i<=3;i++)
system.out.println(“ “ +val1++ + “,” + --val2);
system.out.println(“ “ +val2-- + “,” +++va12);
b. How many times the loop gets executed and write the output.
int x=0;
do{
System.out.print(“Welcome to MYSQL”);
}while(x!=0);

31. Rewrite the following code using while loop :
a. int i,j;
 for(i=1,j=2;i<=6;i++,j+=2)
 System.out.println(i++);
 System.out.println(“Finished!!!”);

 b. Rewrite the following code using do.. while loop
 int sum=0, i=1;
 while(i<=5)
 {
 sum=sum+i;


 }