• ( 1 ) Which Control Statements allow the program to choose different paths of execution?

    • 1) if-else.
    • 2) selection.
    • 3) for
    • 4) None of these
    • Discussion in forum
      Answer : 2) selection.
      Solution :








      discussion


      Answer : 2) selection.

    • ( 2 ) Choose the correct statement in context of the following program code.
      public class Test{
      public static void main(String[] args){
      double sum = 0;
      for(double d = 0; d < 10;){
      d += 0.1;
      sum += sum + d;
      }
      }
      }

    • 1) The program has a compile error because the adjustment is missing in the for loop.
    • 2) The program has a compile error because the control variable in the for loop cannot be of the double type.
    • 3) The program runs in an infinite loop because d<10 would always be true.
    • 4) The program compiles and runs fine.
    • Discussion in forum
      Answer : 4) The program compiles and runs fine.
      Solution :








      discussion


      Answer : 4) The program compiles and runs fine.

    • ( 3 ) What's wrong? while( (i < 10) && (i > 24))

    • 1) the logical operator && cannot be used in a test condition
    • 2) the while loop is an exit-condition loop
    • 3) the test condition is always false
    • 4) the test condition is always true
    • Discussion in forum
      Answer : 3) the test condition is always false
      Solution :








      discussion


      Answer : 3) the test condition is always false

    • ( 4 ) What will be the output of the program?
      public class If1
      {
      static boolean b;
      public static void main(String [] args)
      {
      short hand = 42;
      if ( hand < 50 & !b ) /* Line 7 */
      hand++;
      if ( hand > 50 ); /* Line 9 */
      else if ( hand > 40 )
      {
      hand += 7;
      hand++;
      }
      else
      --hand;
      System.out.println(hand);
      }
      }

    • 1) 41
    • 2) 42
    • 3) 50
    • 4) 51
    • Discussion in forum
      Answer : 4) 51
      Solution : In Java, boolean instance variables are initialized to false, so the if test on line 7 is true and hand is incremented. Line 9 is legal syntax, a do nothing statement. The else-if is true so hand has 7 added to it and is then incremented.








      discussion


      Answer : 4) 51

    • ( 5 ) What is the output of the following program?
      class BreakClass
      {
      public static void main(String[] args)
      {
      for(int i=0;i<5;i++)
      {
      if(i==2)
      {
      break;
      }
      System.out.println("i="+i);
      }
      }
      }

    • 1) i=0
      i=1
    • 2) i=0
      i=1
      i=2
      i=3
      i=4
    • 3) i=0
      i=1
      i=3
      i=4
    • 4) None of these
    • Discussion in forum
      Answer : 1) i=0
      i=1
      Solution :








      discussion


      Answer : 1) i=0
      i=1

    • ( 6 ) What will be the output of the program?
      class Test {
      public static void main(String [] args) {
      int x = 0;
      int y = 0;
      for (int z = 0; z < 5; z++) {
      if (( ++x > 2 ) && (++y > 2)) {
      x++;
      }
      }
      System.out.println(x + " " + y);
      }
      }

    • 1) 5 2
    • 2) 5 3
    • 3) 6 3
    • 4) 6 4
    • Discussion in forum
      Answer : 3) 6 3
      Solution :








      discussion


      Answer : 3) 6 3

    • ( 7 ) 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?

    • 1) 111
    • 2) 000
    • 3) 1111
    • 4) Compiler error
    • Discussion in forum
      Answer : 2) 000
      Solution :








      discussion


      Answer : 2) 000

    • ( 8 ) What will be the output?
      public class Test{
      public static void main(String args[]){
      int i = 1;
      do{
      i--;
      }while(i > 2);
      System.out.println(i);
      }
      }

    • 1) 1
    • 2) 2
    • 3) -1
    • 4) 0
    • Discussion in forum
      Answer : 4) 0
      Solution :








      discussion


      Answer : 4) 0

    • ( 9 ) 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?

    • 1) -2
    • 2) -3
    • 3) Infinite loop
    • 4) Compiler error
    • Discussion in forum
      Answer : 1) -2
      Solution :








      discussion


      Answer : 1) -2

    • ( 10 ) How many times will the following code print "Welcome to Examveda"?
      int count = 0;
      do {
      System.out.println("Welcome to Examveda");
      count++;
      }
      while (count < 10);

    • 1) 8
    • 2) 9
    • 3) 10
    • 4) 11
    • Discussion in forum
      Answer : 3) 10
      Solution :








      discussion


      Answer : 3) 10





Top