Vidyalelo
Java Programming · all questions

Flow Control
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

58

Questions

3/3

Page

Pick an option on a question to see the right answer and solution.

Which of the following for loops will be an infinite loop?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
      a[i] = a[(a[i] + 3) % a.length];

Select an option to see the answer and solution.

What will be the result of compiling and running the following code:
public class Test{
      public static void main(String... args) throws Exception{
            Integer i = 34;
            int l = 34;
            if(i.equals(l)){
                  System.out.println("true");
            }else{
                  System.out.println("false");
            }
      }
}

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What all gets printed when the following program is compiled and run?
public class Test{
      public static void main(String args[]){
            int i=0, j=2;
            do{
                  i=++i;
                  j--;
            }while(j>0);
            System.out.println(i);
      }
}

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

public class Test{
      public static void main(String [] args){
            int x = 0;
            // insert code here
            do{ } while(x++ < y);
            System.out.println(x);
      }
}

Which option, inserted at line 4, produces the output 12?

Select an option to see the answer and solution.

What will be the result?
int i = 10;
while(i++ <= 10){
      i++;
}
System.out.print(i);

Select an option to see the answer and solution.

What is the process by which we can control what parts of a program can access the members of a class?

Select an option to see the answer and solution.

Which of these access specifier must be used for class so that it can be inherited by another subclass?

Select an option to see the answer and solution.

Which of these is used as a default for a member of a class if no access specifier is used for it?

Select an option to see the answer and solution.

Which of these access specifiers must be used for main() method?

Select an option to see the answer and solution.

What will be the output of the following Java code?
class static_out 
{
    static int x;
static int y;
    void add(int a, int b)
    {
        x = a + b;
        y = x + b;
    }
}    
public class static_use 
{
    public static void main(String args[])
    {
        static_out obj1 = new static_out();
        static_out obj2 = new static_out();   
        int a = 2;
        obj1.add(a, a + 1);
        obj2.add(5, a);
        System.out.println(obj1.x + " " + obj2.y);     
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class access
{
    public int x;
private int y;
    void cal(int a, int b)
    {
        x =  a + 1;
        y =  b;
    }        
}    
public class access_specifier 
{
    public static void main(String args[])
    {
        access obj = new access();   
        obj.cal(2, 3);
        System.out.println(obj.x + " " + obj.y);     
    }
}

Select an option to see the answer and solution.

Which of these is used to access a member of class before object of that class is created?

Select an option to see the answer and solution.

Which of the following statements are incorrect?

Select an option to see the answer and solution.

What will be the output of the following Java code?
class access
{
    public int x;
private int y;
    void cal(int a, int b)
    {
        x =  a + 1;
        y =  b;
    }   
    void print() 
    {
        System.out.println(" " + y);     
    } 
}   
public class access_specifier 
{
    public static void main(String args[])
    {
        access obj = new access();   
        obj.cal(2, 3);
        System.out.println(obj.x);
        obj.print();     
    }
}

Select an option to see the answer and solution.