break and continue questions in java

break and continue keyword questions and answers in java


(1)

Which of the following jump statement is not supported by java?

(a) break
(b) goto
(c) continue
(d) return



Answer: (b)

(2)

public class BreakDemo {
    public static void main(String[] args){
         int j=1;
         for(int i=1;i<5;i++){
             j*=i;{
                 break;
             }
         }
         System.out.print(j);
    }
}

What will be output of the above java program?

(a) 1
(b) 4
(c) 24
(d)Compiler error






Answer: (a)

(3)

public class BreakDemo {
    public static void main(String[] args){
         int j=1;
         for(int i=1;i<3;i++){
             j*=i;
             continue;
             ++j;
         }
         System.out.print(j);
    }
}

What will be output of the above java program?

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





Answer: (b)

(4)

public class BreakDemo {
    public static void main(String[] args){
         int j=1;
         for(int i=1;i<3;i++){
             j*=i;
             for(;;){
                 break;
             }
             continue;
         }
         System.out.print(j);
    }
}

What will be output of the above java program?

(a) 2
(b) 3
(c) Infinite loop
(d)Compiler error





Answer: (a)

(5)

public class BreakDemo{
    public static void main(String[] args){
         int j=~-3;
         switch(j){
             default:break;
             case 0:++j;
             case 1:break;
             case 2:j-=2;continue;
         }
    }
}

What will be output of the above java program?

(a)0
(b)1
(c)2
(d)Compiler error






Answer:(d)

(6)

public class BreakDemo {
    public static void main(String[] args){
         int j=~-3;
         while(j<7){
             System.out.print(j);
             if(j==3){
                 j+=2;
                 continue;
             }
             j++;
         }
    }
}

What will be output of the above java program?
         
(a)2367
(b)23
(c)2356
(d)Compiler error





Answer: (c)

(7)

public class BreakDemo {
    public static void main(String[] args){
         int j=~-3;
         while(j<7){
             System.out.println(j);
            
             if(j==3){
                 j+=2;
                 return;
             }
            
             ++j;
         }
    }
}

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





Answer: (b)

(8)

public class BreakDemo {
    public static void main(String[] args){
         String[][]alpha={{"a"},{"b","c"}};
         for(String str[]:alpha){
             for(String s:str){
                 System.out.print(s+" ");
                 if(s.equals("b"))
                    return;
             }
         }
    }
}

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

Answer:(c)

(9)

public class BreakDemo {
    public static void main(String[] args){
         int a=2;
             if(a==2)
                 break first;
             System.out.print("step1");
             first:
             System.out.println("step2");
    }
}

What will be output of the above java program?
         
(a)step1
(b)step2
(c)step2
   Step1
(d)Compiler error





Answer:(d)

(10)

public class BreakDemo {
    public static void main(String[] args){
         int a=2;
         india: {
             ++a;
             if(a==3)
                 break india;
         }
             System.out.print(a);
            
    }
}

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





Answer: (a)

1 comment:

ram said...

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!

data science course in vizag