Looping questions in java

Objective type mcq questions on loops in java and answers

(1)

public class Loop {
    public static void main(String[] args) {
         Integer a=012,b;
         for(b=0;b<=a;b++);
         System.out.print(b);
    }
}

What will be the output of above java program?

(a)10
(b)11
(c)12
(d) Compiler error





Answer: (b)

(2)

public class Loop {
    public static void main(String[] args){
         int b=0;
         do{
            int a=2;
            b++;
            System.out.println(a++);
         }
         while(b!=3);
    }
}

What will be the output of above java program?

(a)2
   2
   2
(b)3
   3
   3
(c)3
   4
   5
(d) Declaration is not allowed here, Compiler error






Answer: (a)

(3)

public class Loop {
    public static void main(String[] args){
         for(int a=0,b=0;a+b<5;a++,++b){
             System.out.print(a^b);
         }
    }
}

What will be the output of above java program?

(a)111
(b)000
(c)1111
(d)Compiler error






Answer: (b)

(4)

public class Loop {
    public static void main(String[] args){
        int i=0;
        for(;i<4;i++){
          System.out.println(i<2);
         }
    }
}

What will be output of above program?

(a)Infinite loop
(b) true
    false
    false
    false
(c) true
    true
    false
    false
(d)Compiler error






Answer: (c)

(5)

public class Loop {
    public static void main(String[] args){
    int i=0;
        for(i++;i<4;i++){
          System.out.println(~i);
        }
    }
}

What will be the output of above java program?

(a)-1
   -3
(b)-2
(c)-2
   -3
   -4
(d)Compiler error






Answer :( c)

(6)

public class Loop {
    public static void main(String[] args){
         int a[]={06,07,010,011};
         for(int value:a)
             System.out.println(value);
    }
}

What will be the output of above java program?
         
(a)6
   7
   10
   11
(b)6
   7
   8
   9
(c)06
   07
   010
   011
(d)Compiler error





Answer: (b)

(7)

public class Loop {
    public static void main(String[] args){
         char a[]={'\10','\11','\12','\13'};
         for(int value:a)
             System.out.println(value);
    }
}

What will be the output of above java program?
         
(a)10
   11
   12
   13
(b)It will print corresponding ASCII character
(c)8
   9
   10
   11
(d)Compiler error





Answer: (c)

(8)

public class Loop {
    public static void main(String[] args){
         String str[]={"a","b","c","d"};
         for(String value:str){
             value="q";
         }
         System.out.print(str[0]+str[1]+str[2]);
    }
}

What will be the output of above java program?
         
(a)qqq
(b)abc
(c)Run time exception
(d)Compiler error






Answer: (b)

(9)

public class Loop {
    public static void main(String[] args){
         Double d=1.5D;
         for(int i=2;i>=-1;){
             System.out.println(d/i);
             --i;
             --d;
         }
    }
}

What will be the output of above java program?
         
(a) 0.75
    0.5
    -Infinity
    1.5
(b) 0.75
    0.5
(c) 0.75
    0.5
    NaN
    1.5
(d)Compiler error





Answer: (a)

(10)

public class Loop {
    public static void main(String[] args){
         for(int i=0;false;i++)
         {
             System.out.println("java");
         }
    }
}

What will be the output of above java program?
         
(a)java
(b)null
(c)It will not print anything.
(d)Compiler error





Answer: (d)

(11)

public class Loop {
    public static void main(String[] args){
         int i=0;
         boolean b=true;
         for(i++;b;i++){
             System.out.println(~i);
             b^=true;
         }
    }
}

What will be the output of above java program?
         
(a)-2
(b)-3
(c)Infinite loop
(d)Compiler error





Answer: (a)

(12)

public class Loop {
    public static void main(String[] args){
         int i=0;
         int j=5;
         boolean b=true;
         for(i++,--j;b;){
             System.out.println(~i-~j);
             b^=true;
         }
    }
}

What will be the output of above java program?
         
(a)3
(b)4
(c)Infinite loop
(d)Compiler error






Answer: (a)

(13)

public class Loop {
    public static void main(String[] args){
         int i=0;
         int j=05;
         for(i++,--j;j>0;){
             System.out.println(j>>>i);
             --j;
         }
    }
}

What will be the output of above java program?
         
(a)2
   1
   1
   0
(b)2
   0
(c)2
   0
   0
(d)Compiler error






Answer :( a)

(14)

public class Loop {
    public static void main(String[] args){
         int i=1;
         int j=5;
         for(;;){
             System.out.println(j>>>i);
         }
    }
}

What will be the output of above java program?
         
(a)2
   1
(b)2
   1
   0
(c)Infinite loop
(d)Compiler error






Answer: (c)

(15)

public class Loop {
    public static void main(String[] args){
         Double d=5.0;
         for(int i=2;i>=-1;){
             System.out.println(d/i);
             --i;
         }
    }
}

What will be the output of above java program?
         
(a)2.5
   5.0
(b)2.5
   5.0
   Infinity
   -5.0
(c)Run time exception
(d)Compiler error






Post a Comment