Java - MCQ

Multiple Choice Questions


Share with a Friend

 

Java MCQ - Loops, Decisions and Mathematical functions - Set 23

16. What will be the value of y after execution of switch statement?

public class Test{
        public static void main(String[] args){
                int x = 3, y = 4;
                switch(x + 3){
                        case 6: y = 0;
                        case 7: y = 1;
                        default: y += 1;
                }
        }
}
A). 1
B). 2
C). 3
D). 4
View Answer
Correct: B



17. What is the printout of the following switch statement?

char ch = 'a';  
switch (ch){
      case 'a':
      case 'A': System.out.print(ch); break;
      case 'b':
      case 'B': System.out.print(ch); break;
      case 'c':
      case 'C': System.out.print(ch); break;
      case 'd':
      case 'D': System.out.print(ch);
}
A). abcd
B). aa
C). a
D). ab
View Answer
Correct: C



18. How many times will the following code print "Welcome to DIET"?

int count = 0;
do {
      System.out.println("Welcome to DIET");
      count++;
} while (count < 10);
A). 8
B). 9
C). 10
D). 11
View Answer
Correct: C



19. What will be the result of the following code?

public class Test{
      static public void main(String args[]){ //line 2
            int i, j;
            for(i=0; i<3; i++){
                  for(j=1; j<4; j++){
                        i%=j;
                        System.out.println(j);
                  }
            }
      }
}
A). 1 2 3 1
B). 1 2 3 2
C). Repeatedly print 1 2 3 and cause infinite loop.
D). Compilation fails because of line 2
View Answer
Correct: C



20. What all gets printed when the following program is compiled and run.

public class Test{
      public static void main(String args[]){ 
            int i, j=1;
            i = (j>1)?2:1;
            switch(i){
                  case 0: System.out.println(0); break;
                  case 1: System.out.println(1);
                  case 2: System.out.println(2); break;
                  case 3: System.out.println(3); break;
            }
      }
}
A). 0
B). 1
C). 2
D). 1 2
View Answer
Correct: D



 
 
 
   
   
   
UltraInfo Technologies
ITDeveloper